From 1544fdaf4379c255a6a144a3e3a6cdccd1b3cbbf Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:16:13 +0800 Subject: [PATCH] fix: parse xml element --- src/data-types.tsx | 2 +- src/{utils.tsx => utils.ts} | 0 src/y-type.ts | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) rename src/{utils.tsx => utils.ts} (100%) diff --git a/src/data-types.tsx b/src/data-types.tsx index 7f06d50..3af2027 100644 --- a/src/data-types.tsx +++ b/src/data-types.tsx @@ -45,7 +45,7 @@ const YTypePreComponent = ({ showDelta: config.showDelta, }); if (typeof parsedValue === "string") { - return null; + return ; } return ( diff --git a/src/utils.tsx b/src/utils.ts similarity index 100% rename from src/utils.tsx rename to src/utils.ts diff --git a/src/y-type.ts b/src/y-type.ts index 0bc8b42..026217f 100644 --- a/src/y-type.ts +++ b/src/y-type.ts @@ -75,6 +75,10 @@ export function isYXmlFragment(value: unknown): value is Y.XmlFragment { return value instanceof Y.XmlFragment; } +export function isYXmlText(value: unknown): value is Y.XmlText { + return value instanceof Y.XmlText; +} + export function isYAbstractType( value: unknown, ): value is Y.AbstractType { @@ -130,6 +134,25 @@ export function parseYType( return value.toString(); } + if (isYXmlElement(value)) { + return { + nodeName: value.nodeName, + attributes: value.getAttributes(), + "toString()": value.toString(), + }; + } + + if (isYXmlFragment(value)) { + return value.toJSON(); + } + + if (isYXmlText(value)) { + if (showDelta) { + return value.toDelta(); + } + return value.toString(); + } + return value; }