@synerise/ds-rich-text 2.0.11 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.2.0](https://github.com/Synerise/synerise-design/compare/@synerise/ds-rich-text@2.1.0...@synerise/ds-rich-text@2.2.0) (2026-10-29)
7
+
8
+
9
+ ### Features
10
+
11
+ * integrate Token Studio design tokens across the design system ([aabb447](https://github.com/Synerise/synerise-design/commit/aabb447edfb2d0c85e3de1f07e809ef84083453b))
12
+
13
+
14
+
15
+
16
+
17
+ # [2.1.0](https://github.com/Synerise/synerise-design/compare/@synerise/ds-rich-text@2.0.11...@synerise/ds-rich-text@2.1.0) (2026-10-28)
18
+
19
+
20
+ ### Features
21
+
22
+ * **rich-text:** support markdown paste ([de3bf47](https://github.com/Synerise/synerise-design/commit/de3bf4795cf5682156090e7365e18d6c58226946))
23
+
24
+
25
+
26
+
27
+
6
28
  ## [2.0.11](https://github.com/Synerise/synerise-design/compare/@synerise/ds-rich-text@2.0.10...@synerise/ds-rich-text@2.0.11) (2026-10-26)
7
29
 
8
30
 
package/README.md CHANGED
@@ -44,6 +44,21 @@ const [doc, setDoc] = useState<RichTextDocument>();
44
44
  />
45
45
  ```
46
46
 
47
+ ### Markdown mode
48
+
49
+ ```tsx
50
+ <RichText
51
+ format="markdown"
52
+ value={markdown}
53
+ onChange={(markdown) => setMarkdown(markdown)}
54
+ />
55
+ ```
56
+
57
+ - **Load:** `value` is read as markdown. Inline HTML the editor writes (`<u>`, `<br>`) is supported.
58
+ - **Paste:** pasted plain text is parsed as markdown, and every pasted line stays a line. HTML tags in it stay text. Rich content copied from documents or another editor is pasted as is.
59
+ - **Save:** line breaks are saved as `<br>`, and `\`, `<` and `&` in text are escaped (`List\<String>`).
60
+ - Only the formats in `toolbarFeatures` are kept. The text of any other block stays as plain paragraphs.
61
+
47
62
  ### With image upload
48
63
 
49
64
  ```tsx
@@ -74,7 +89,7 @@ const [doc, setDoc] = useState<RichTextDocument>();
74
89
 
75
90
  | Property | Description | Type | Default |
76
91
  | --- | --- | --- | --- |
77
- | format | Output format — `'html'` emits string, `'document'` emits `RichTextDocument` | `'html' \| 'document'` | `'html'` |
92
+ | format | Output format — `'html'` emits an HTML string, `'markdown'` a markdown string, `'document'` a `RichTextDocument` | `'html' \| 'markdown' \| 'document'` | `'html'` |
78
93
  | value | Controlled value (accepts both HTML string and `RichTextDocument`) | `string \| RichTextDocument` | - |
79
94
  | defaultValue | Initial value for uncontrolled mode | `string \| RichTextDocument` | - |
80
95
  | onChange | Called when content changes | `(value: string \| RichTextDocument) => void` | - |
package/dist/RichText.js CHANGED
@@ -1,28 +1,33 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useEditor, EditorContent } from "@tiptap/react";
3
3
  import { forwardRef, useState, useRef, useMemo, useCallback, useEffect } from "react";
4
- import { useTheme } from "@synerise/ds-core";
5
4
  import { FormFieldLabel } from "@synerise/ds-form-field";
6
5
  import Icon, { EditS } from "@synerise/ds-icon";
7
6
  import { useCombinedRefs, useOnClickOutside } from "@synerise/ds-utils";
8
7
  import { documentToMarkdown } from "./blockModel/serializers/documentToMarkdown.js";
9
8
  import { documentToTiptapJson } from "./blockModel/serializers/documentToTiptap.js";
10
- import { markdownToDocument } from "./blockModel/serializers/markdownToDocument.js";
9
+ import "./blockModel/serializers/markdownToDocument.js";
11
10
  import { tiptapJsonToDocument } from "./blockModel/serializers/tiptapToDocument.js";
12
11
  import "@synerise/ds-button";
13
12
  import "@synerise/ds-input";
14
13
  import { RichTextWrapper, ContentAbove, EditorWrapper, ToolbarArea, EditorArea, SubtleSuffix, ContentBelow, ErrorText, Description } from "./RichText.styles.js";
15
14
  import { getExtensions } from "./utils/getExtensions.js";
15
+ import { handleMarkdownPaste } from "./utils/handleMarkdownPaste.js";
16
+ import { markdownToHtml } from "./utils/markdownToHtml.js";
16
17
  import { Toolbar } from "./components/Toolbar.js";
17
18
  import { DEFAULT_TEXTS, ALL_TOOLBAR_FEATURES, DEFAULT_HEADING_LEVELS } from "./RichText.types.js";
18
19
  const SUBTLE_IGNORE_SELECTORS = ["[data-popover-content]"];
20
+ const MARKDOWN_EDITOR_PROPS = {
21
+ handlePaste: handleMarkdownPaste
22
+ };
23
+ const DEFAULT_EDITOR_PROPS = {};
19
24
  const isDocument = (val) => typeof val === "object" && val !== null && val.type === "doc";
20
25
  const toEditorContent = (val, format) => {
21
26
  if (val === void 0) {
22
27
  return "";
23
28
  }
24
29
  if (typeof val === "string") {
25
- return format === "markdown" ? documentToTiptapJson(markdownToDocument(val)) : val;
30
+ return format === "markdown" ? markdownToHtml(val) : val;
26
31
  }
27
32
  return documentToTiptapJson(val);
28
33
  };
@@ -61,7 +66,6 @@ const RichText = forwardRef(({
61
66
  const [subtleBlurred, setSubtleBlurred] = useState(false);
62
67
  const lastEmittedRef = useRef("");
63
68
  const wrapperRef = useCombinedRefs(ref);
64
- const theme = useTheme();
65
69
  const hasError = !!errorText;
66
70
  const isSubtle = subtle && !readOnly && !disabled;
67
71
  const isSubtlePreview = isSubtle && !subtleActive && !hasError;
@@ -103,6 +107,7 @@ const RichText = forwardRef(({
103
107
  content: toEditorContent(value ?? defaultValue, format),
104
108
  editable: isEditable,
105
109
  autofocus: autoFocus,
110
+ editorProps: format === "markdown" ? MARKDOWN_EDITOR_PROPS : DEFAULT_EDITOR_PROPS,
106
111
  onUpdate: handleUpdate,
107
112
  onFocus: () => setIsFocused(true),
108
113
  onBlur: () => setIsFocused(false)
@@ -147,7 +152,7 @@ const RichText = forwardRef(({
147
152
  }
148
153
  } else if (format === "markdown") {
149
154
  if (lastEmittedRef.current !== value) {
150
- editor.commands.setContent(documentToTiptapJson(markdownToDocument(value)), false);
155
+ editor.commands.setContent(markdownToHtml(value), false);
151
156
  }
152
157
  } else {
153
158
  const currentHTML = editor.getHTML();
@@ -165,7 +170,7 @@ const RichText = forwardRef(({
165
170
  /* @__PURE__ */ jsxs(EditorWrapper, { $hasError: hasError, $noBorder: noBorder, $isFocused: isFocused, $subtlePreview: isSubtlePreview, $subtleBlurred: subtleBlurred, $animations: animations, onClick: isSubtlePreview ? handleSubtleActivate : void 0, "data-testid": "rich-text-editor-wrapper", children: [
166
171
  !hideToolbar && !readOnly && !isSubtlePreview && /* @__PURE__ */ jsx(ToolbarArea, { children: /* @__PURE__ */ jsx(Toolbar, { editor, features: toolbarFeatures, headingLevels, texts, disabled, onImageUpload, onEditWithAI, editWithAIOptions }) }),
167
172
  /* @__PURE__ */ jsx(EditorArea, { $height: height, $maxHeight: maxHeight, $readOnly: readOnly, $subtlePreview: isSubtlePreview, "data-testid": "rich-text-editor", children: /* @__PURE__ */ jsx(EditorContent, { editor }) }),
168
- isSubtlePreview && /* @__PURE__ */ jsx(SubtleSuffix, { "data-testid": "rich-text-subtle-edit-icon", children: /* @__PURE__ */ jsx(Icon, { component: /* @__PURE__ */ jsx(EditS, {}), color: theme.palette["grey-600"] }) })
173
+ isSubtlePreview && /* @__PURE__ */ jsx(SubtleSuffix, { "data-testid": "rich-text-subtle-edit-icon", children: /* @__PURE__ */ jsx(Icon, { component: /* @__PURE__ */ jsx(EditS, {}) }) })
169
174
  ] }),
170
175
  (errorText || description) && /* @__PURE__ */ jsxs(ContentBelow, { "data-testid": "rich-text-below", children: [
171
176
  errorText && /* @__PURE__ */ jsx(ErrorText, { "data-testid": "rich-text-error", children: errorText }),
@@ -1,4 +1,3 @@
1
- import { ThemeProps } from '@synerise/ds-core';
2
1
  export declare const RichTextWrapper: import('styled-components').StyledComponent<"div", any, {
3
2
  $disabled?: boolean;
4
3
  }, never>;
@@ -11,7 +10,7 @@ export declare const EditorWrapper: import('styled-components').StyledComponent<
11
10
  $subtleBlurred?: boolean;
12
11
  $animations?: boolean;
13
12
  }, never>;
14
- export declare const ToolbarArea: import('styled-components').StyledComponent<"div", any, ThemeProps, never>;
13
+ export declare const ToolbarArea: import('styled-components').StyledComponent<"div", any, {}, never>;
15
14
  export declare const ToolbarItems: import('styled-components').StyledComponent<"div", any, {}, never>;
16
15
  export declare const ToolbarEntry: import('styled-components').StyledComponent<"span", any, {}, never>;
17
16
  export declare const EditorArea: import('styled-components').StyledComponent<"div", any, {
@@ -22,10 +21,10 @@ export declare const EditorArea: import('styled-components').StyledComponent<"di
22
21
  }, never>;
23
22
  export declare const ContentAbove: import('styled-components').StyledComponent<"div", any, {}, never>;
24
23
  export declare const ContentBelow: import('styled-components').StyledComponent<"div", any, {}, never>;
25
- export declare const ErrorText: import('styled-components').StyledComponent<"div", any, ThemeProps, never>;
26
- export declare const Description: import('styled-components').StyledComponent<"div", any, ThemeProps, never>;
24
+ export declare const ErrorText: import('styled-components').StyledComponent<"div", any, {}, never>;
25
+ export declare const Description: import('styled-components').StyledComponent<"div", any, {}, never>;
27
26
  export declare const AiGradientLabel: import('styled-components').StyledComponent<"span", any, {}, never>;
28
- export declare const ToolbarSeparator: import('styled-components').StyledComponent<"div", any, ThemeProps, never>;
27
+ export declare const ToolbarSeparator: import('styled-components').StyledComponent<"div", any, {}, never>;
29
28
  export declare const BlockMenu: import('styled-components').StyledComponent<"div", any, {}, never>;
30
29
  export declare const PopoverForm: import('styled-components').StyledComponent<"div", any, {}, never>;
31
30
  export declare const PopoverActions: import('styled-components').StyledComponent<"div", any, {}, never>;
@@ -1,23 +1,22 @@
1
1
  import styled, { css, keyframes } from "styled-components";
2
- import { hexToRgba } from "@synerise/ds-utils";
3
2
  const TOOLBAR_HEIGHT_PX = 44;
4
3
  const subtleBlurAnimation = /* @__PURE__ */ keyframes(["0%{padding-top:", "px;}100%{padding-top:12px;}"], 12 + TOOLBAR_HEIGHT_PX);
5
4
  const RichTextWrapper = /* @__PURE__ */ styled.div.withConfig({
6
5
  displayName: "RichTextstyles__RichTextWrapper",
7
6
  componentId: "sc-8lgglv-0"
8
- })(["display:flex;flex-direction:column;", ";"], (props) => props.$disabled && css(["opacity:0.4;pointer-events:none;"]));
7
+ })(["display:flex;flex-direction:column;", ";"], (props) => props.$disabled && css(["opacity:var(--ds-opacity-disabled);pointer-events:none;"]));
9
8
  const SubtleSuffix = /* @__PURE__ */ styled.div.withConfig({
10
9
  displayName: "RichTextstyles__SubtleSuffix",
11
10
  componentId: "sc-8lgglv-1"
12
- })(["position:absolute;right:6px;top:6px;display:flex;opacity:0;height:24px;transition:opacity 0.1s ease-in;transition-delay:0.2s;pointer-events:none;"]);
11
+ })(["position:absolute;right:6px;top:6px;display:flex;color:var(--ds-color-icon-base-default);opacity:0;height:24px;transition:opacity 0.1s ease-in;transition-delay:0.2s;pointer-events:none;"]);
13
12
  const EditorWrapper = /* @__PURE__ */ styled.div.withConfig({
14
13
  displayName: "RichTextstyles__EditorWrapper",
15
14
  componentId: "sc-8lgglv-2"
16
- })(["position:relative;border:", ";border-radius:3px;display:flex;flex-direction:column;overflow:hidden;transition:border-color 0.2s ease,background 0.1s ease-in;", ";", ";", ";", ";"], (props) => props.$noBorder ? "none" : props.$subtlePreview ? "solid 1px transparent" : `solid 1px ${props.$hasError ? props.theme.palette["red-600"] : props.$isFocused ? props.theme.palette["blue-600"] : props.theme.palette["grey-200"]}`, (props) => props.$subtlePreview && css(["cursor:pointer;&& .ProseMirror{cursor:pointer;padding-left:0;}&&:hover{background:", ";", "{opacity:1;}}", ";"], hexToRgba(props.theme.palette["grey-300"], 0.4), SubtleSuffix, props.$animations ? css(["transition:border-color 0.2s ease,background 0.1s ease-in 0.2s;&& .ProseMirror{transition:padding 0.1s ease-in 0.2s;}&& .ProseMirror p.is-editor-empty:first-child::before{transform:translateY(-6px);transition:transform 0.1s ease-in 0.2s;}&&:hover{.ProseMirror{padding-left:12px;}.ProseMirror p.is-editor-empty:first-child::before{transform:translateY(0);}}"]) : css(["transition:border-color 0.2s ease;", "{transition:none;}"], SubtleSuffix)), (props) => props.$subtlePreview && props.$subtleBlurred && props.$animations && css(["&& .ProseMirror{animation:", " 0.1s ease-in 0.2s backwards;}"], subtleBlurAnimation), (props) => props.$hasError && css(["background:", ";", ";"], props.theme.palette["red-050"], !props.$noBorder && css(["outline:1px solid ", ";outline-offset:-1px;"], props.theme.palette["red-600"])), (props) => props.$isFocused && !props.$hasError && !props.$noBorder && css(["outline:1px solid ", ";outline-offset:-1px;"], props.theme.palette["blue-600"]));
15
+ })(["position:relative;border:", ";border-radius:3px;display:flex;flex-direction:column;overflow:hidden;transition:border-color 0.2s ease,background 0.1s ease-in;", ";", ";", ";", ";"], (props) => props.$noBorder ? "none" : props.$subtlePreview ? "solid 1px transparent" : `solid 1px ${props.$hasError ? "var(--ds-color-border-danger-default)" : props.$isFocused ? "var(--ds-color-focus-base-default)" : "var(--ds-color-border-base-default)"}`, (props) => props.$subtlePreview && css(["cursor:pointer;&& .ProseMirror{cursor:pointer;padding-left:0;}&&:hover{background:var(--ds-rich-text-bg-hover);", "{opacity:1;}}", ";"], SubtleSuffix, props.$animations ? css(["transition:border-color 0.2s ease,background 0.1s ease-in 0.2s;&& .ProseMirror{transition:padding 0.1s ease-in 0.2s;}&& .ProseMirror p.is-editor-empty:first-child::before{transform:translateY(-6px);transition:transform 0.1s ease-in 0.2s;}&&:hover{.ProseMirror{padding-left:12px;}.ProseMirror p.is-editor-empty:first-child::before{transform:translateY(0);}}"]) : css(["transition:border-color 0.2s ease;", "{transition:none;}"], SubtleSuffix)), (props) => props.$subtlePreview && props.$subtleBlurred && props.$animations && css(["&& .ProseMirror{animation:", " 0.1s ease-in 0.2s backwards;}"], subtleBlurAnimation), (props) => props.$hasError && css(["background:var(--ds-color-background-danger-subtle);", ";"], !props.$noBorder && css(["outline:1px solid var(--ds-color-border-danger-default);outline-offset:-1px;"])), (props) => props.$isFocused && !props.$hasError && !props.$noBorder && css(["outline:1px solid var(--ds-color-focus-base-default);outline-offset:-1px;"]));
17
16
  const ToolbarArea = /* @__PURE__ */ styled.div.withConfig({
18
17
  displayName: "RichTextstyles__ToolbarArea",
19
18
  componentId: "sc-8lgglv-3"
20
- })(["padding:4px;box-shadow:inset 0 -1px 0 0 ", ";> div{> div{flex:0 0 auto;background:transparent;box-shadow:none;padding:2px;border-radius:0;}}"], (props) => props.theme.palette["grey-200"]);
19
+ })(["padding:4px;box-shadow:inset 0 -1px 0 0 var(--ds-color-border-base-default);> div{> div{flex:0 0 auto;background:transparent;box-shadow:none;padding:2px;border-radius:0;}}"]);
21
20
  const ToolbarItems = /* @__PURE__ */ styled.div.withConfig({
22
21
  displayName: "RichTextstyles__ToolbarItems",
23
22
  componentId: "sc-8lgglv-4"
@@ -29,7 +28,7 @@ const ToolbarEntry = /* @__PURE__ */ styled.span.withConfig({
29
28
  const EditorArea = /* @__PURE__ */ styled.div.withConfig({
30
29
  displayName: "RichTextstyles__EditorArea",
31
30
  componentId: "sc-8lgglv-6"
32
- })([".ProseMirror{padding:12px;outline:none;min-height:", ";height:", ";max-height:", ";overflow-y:auto;font-size:13px;line-height:1.6;color:", ";", ";> * + *{margin-top:0.5em;}h1{font-size:24px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}h2{font-size:20px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}h3{font-size:16px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}p{margin:0 0 4px;}ul,ol{padding-left:24px;margin:0 0 4px;}li{margin:2px 0;}a{color:", ";text-decoration:underline;cursor:pointer;}code{background:", ";padding:2px 4px;border-radius:3px;font-family:'SFMono-Regular',Consolas,'Liberation Mono',Menlo,monospace;font-size:12px;}pre{background:", ";padding:12px;border-radius:3px;overflow-x:auto;code{background:none;padding:0;border-radius:0;font-size:12px;}}blockquote{border-left:3px solid ", ";padding-left:12px;margin:0 0 4px;color:", ";}.ds-rt-code-snippet{position:relative;display:flex;align-items:center;background:", ";border-radius:3px;padding:6px 40px 6px 12px;margin:0 0 4px;code{flex:1 1 auto;background:none;padding:0;border-radius:0;white-space:pre;overflow-x:auto;outline:none;}.ds-rt-code-snippet__actions{position:absolute;top:50%;right:4px;transform:translateY(-50%);}}table{border-collapse:collapse;width:100%;margin:0 0 4px;table-layout:fixed;overflow:hidden;td,th{border:1px solid ", ";padding:6px 10px;vertical-align:top;text-align:left;position:relative;> *{margin:0;}}th{background:", ";font-weight:500;}.selectedCell::after{content:'';position:absolute;inset:0;background:", ";opacity:0.4;pointer-events:none;}}s{text-decoration:line-through;}img{max-width:100%;height:auto;border-radius:3px;display:block;margin:8px 0;&.ProseMirror-selectednode{outline:2px solid ", ";outline-offset:2px;}}}.ProseMirror p.is-editor-empty:first-child::before{color:", ";content:attr(data-placeholder);float:left;height:0;pointer-events:none;}"], (props) => props.$subtlePreview ? "32px" : "120px", (props) => props.$height ? typeof props.$height === "number" ? `${props.$height}px` : props.$height : "auto", (props) => props.$maxHeight ? typeof props.$maxHeight === "number" ? `${props.$maxHeight}px` : props.$maxHeight : "none", (props) => props.theme.palette["grey-800"], (props) => props.$readOnly && css(["background:", ";cursor:default;"], props.theme.palette["grey-050"]), (props) => props.theme.palette["blue-600"], (props) => props.theme.palette["grey-100"], (props) => props.theme.palette["grey-100"], (props) => props.theme.palette["grey-300"], (props) => props.theme.palette["grey-500"], (props) => props.theme.palette["grey-050"], (props) => props.theme.palette["grey-300"], (props) => props.theme.palette["grey-050"], (props) => props.theme.palette["blue-050"], (props) => props.theme.palette["blue-600"], (props) => props.theme.palette["grey-400"]);
31
+ })([".ProseMirror{padding:12px;outline:none;min-height:", ";height:", ";max-height:", ";overflow-y:auto;font-size:13px;line-height:1.6;color:var(--ds-color-text-base-default);", ";> * + *{margin-top:0.5em;}h1{font-size:24px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}h2{font-size:20px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}h3{font-size:16px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}p{margin:0 0 4px;}ul,ol{padding-left:24px;margin:0 0 4px;}li{margin:2px 0;}a{color:var(--ds-color-text-brand-default);text-decoration:underline;cursor:pointer;}code{background:var(--ds-color-background-base-muted);padding:2px 4px;border-radius:3px;font-family:'SFMono-Regular',Consolas,'Liberation Mono',Menlo,monospace;font-size:12px;}pre{background:var(--ds-color-background-base-muted);padding:12px;border-radius:3px;overflow-x:auto;code{background:none;padding:0;border-radius:0;font-size:12px;}}blockquote{border-left:3px solid var(--ds-color-border-base-strong);padding-left:12px;margin:0 0 4px;color:var(--ds-color-text-neutral-default);}.ds-rt-code-snippet{position:relative;display:flex;align-items:center;background:var(--ds-color-background-base-subtle);border-radius:3px;padding:6px 40px 6px 12px;margin:0 0 4px;code{flex:1 1 auto;background:none;padding:0;border-radius:0;white-space:pre;overflow-x:auto;outline:none;}.ds-rt-code-snippet__actions{position:absolute;top:50%;right:4px;transform:translateY(-50%);}}table{border-collapse:collapse;width:100%;margin:0 0 4px;table-layout:fixed;overflow:hidden;td,th{border:1px solid var(--ds-color-border-base-strong);padding:6px 10px;vertical-align:top;text-align:left;position:relative;> *{margin:0;}}th{background:var(--ds-color-background-base-subtle);font-weight:500;}.selectedCell::after{content:'';position:absolute;inset:0;background:var(--ds-color-background-brand-subtle);opacity:0.4;pointer-events:none;}}s{text-decoration:line-through;}img{max-width:100%;height:auto;border-radius:3px;display:block;margin:8px 0;&.ProseMirror-selectednode{outline:2px solid var(--ds-color-focus-base-default);outline-offset:2px;}}}.ProseMirror p.is-editor-empty:first-child::before{color:var(--ds-color-text-base-disabled);content:attr(data-placeholder);float:left;height:0;pointer-events:none;}"], (props) => props.$subtlePreview ? "32px" : "120px", (props) => props.$height ? typeof props.$height === "number" ? `${props.$height}px` : props.$height : "auto", (props) => props.$maxHeight ? typeof props.$maxHeight === "number" ? `${props.$maxHeight}px` : props.$maxHeight : "none", (props) => props.$readOnly && css(["background:var(--ds-color-background-base-subtle);cursor:default;"]));
33
32
  const ContentAbove = /* @__PURE__ */ styled.div.withConfig({
34
33
  displayName: "RichTextstyles__ContentAbove",
35
34
  componentId: "sc-8lgglv-7"
@@ -41,20 +40,20 @@ const ContentBelow = /* @__PURE__ */ styled.div.withConfig({
41
40
  const ErrorText = /* @__PURE__ */ styled.div.withConfig({
42
41
  displayName: "RichTextstyles__ErrorText",
43
42
  componentId: "sc-8lgglv-9"
44
- })(["color:", ";font-size:13px;line-height:1.39;"], (props) => props.theme.palette["red-600"]);
43
+ })(["color:var(--ds-color-text-danger-default);font-size:13px;line-height:1.39;"]);
45
44
  const Description = /* @__PURE__ */ styled.div.withConfig({
46
45
  displayName: "RichTextstyles__Description",
47
46
  componentId: "sc-8lgglv-10"
48
- })(["color:", ";font-size:13px;line-height:1.39;"], (props) => props.theme.palette["grey-500"]);
47
+ })(["color:var(--ds-color-text-neutral-default);font-size:13px;line-height:1.39;"]);
49
48
  const AiGradientLabel = /* @__PURE__ */ styled.span.withConfig({
50
49
  displayName: "RichTextstyles__AiGradientLabel",
51
50
  componentId: "sc-8lgglv-11"
52
- })(["background:linear-gradient( 90deg,", " 0%,", " 100% );-webkit-background-clip:text;background-clip:text;-webkit-text-fill-color:transparent;"], (props) => props.theme.palette["mars-500"], (props) => props.theme.palette["purple-400"]);
51
+ })(["background:var(--ds-rich-text-ai-label-text);-webkit-background-clip:text;background-clip:text;-webkit-text-fill-color:transparent;"]);
53
52
  const ToolbarSeparator = /* @__PURE__ */ styled.div.withConfig({
54
53
  displayName: "RichTextstyles__ToolbarSeparator",
55
54
  componentId: "sc-8lgglv-12"
56
- })(["&&{align-self:center;flex:0 0 auto;width:1px;height:18px;min-height:18px;margin:0 4px;padding:0;border-radius:0;background:", ";}"], (props) => props.theme.palette["grey-200"]);
57
- const popoverSurface = /* @__PURE__ */ css(["background:", ";border:1px solid ", ";border-radius:3px;box-shadow:0 4px 12px 0 rgba(0,0,0,0.12);"], (props) => props.theme.palette.white, (props) => props.theme.palette["grey-200"]);
55
+ })(["&&{align-self:center;flex:0 0 auto;width:1px;height:18px;min-height:18px;margin:0 4px;padding:0;border-radius:0;background:var(--ds-color-border-base-default);}"]);
56
+ const popoverSurface = /* @__PURE__ */ css(["background:var(--ds-color-background-base-default);border:1px solid var(--ds-color-border-base-default);border-radius:3px;box-shadow:var(--ds-shadows-shadow-1);"]);
58
57
  const BlockMenu = /* @__PURE__ */ styled.div.withConfig({
59
58
  displayName: "RichTextstyles__BlockMenu",
60
59
  componentId: "sc-8lgglv-13"
@@ -14,11 +14,13 @@ const serializeMark = (mark, inner) => {
14
14
  return `[${inner}](${mark.href})`;
15
15
  }
16
16
  };
17
+ const escapeText = (text) => text.replace(/[\\<&]/g, "\\$&");
17
18
  const serializeInline = (node) => {
18
19
  if (node.type === "hardBreak") {
19
- return " \n";
20
+ return "<br>";
20
21
  }
21
- let text = node.text;
22
+ const isCode = node.marks?.some((mark) => mark.type === "code");
23
+ let text = isCode ? node.text : escapeText(node.text);
22
24
  if (node.marks) {
23
25
  for (const mark of node.marks) {
24
26
  text = serializeMark(mark, text);
@@ -27,15 +29,18 @@ const serializeInline = (node) => {
27
29
  return text;
28
30
  };
29
31
  const serializeInlines = (children) => children.map(serializeInline).join("");
32
+ const serializeCode = (children) => children.map((node) => node.type === "text" ? node.text : "\n").join("");
30
33
  const indentLines = (text, indent) => text.split("\n").map((line) => line ? indent + line : line).join("\n");
31
34
  const serializeListItem = (item, marker, indent) => {
32
35
  const [first, ...rest] = item.children;
33
36
  const firstText = first ? serializeBlock(first) : "";
34
37
  const continuation = indent + " ".repeat(marker.length);
35
38
  let line = `${indent}${marker}${firstText}`;
39
+ let previous = first;
36
40
  for (const child of rest) {
37
- line += `
38
- ${indentLines(serializeBlock(child), continuation)}`;
41
+ const isAfterList = previous?.type === "bulletList" || previous?.type === "orderedList";
42
+ line += `${isAfterList ? "\n\n" : "\n"}${indentLines(serializeBlock(child), continuation)}`;
43
+ previous = child;
39
44
  }
40
45
  return line;
41
46
  };
@@ -47,10 +52,10 @@ const serializeBlock = (block, indent = "") => {
47
52
  return `${"#".repeat(block.attrs.level)} ${serializeInlines(block.children)}`;
48
53
  case "codeBlock":
49
54
  return `\`\`\`
50
- ${serializeInlines(block.children)}
55
+ ${serializeCode(block.children)}
51
56
  \`\`\``;
52
57
  case "codeSnippet":
53
- return `\`${serializeInlines(block.children)}\``;
58
+ return `\`${serializeCode(block.children)}\``;
54
59
  case "image":
55
60
  return `![${block.attrs.alt ?? ""}](${block.attrs.src})`;
56
61
  case "bulletList":
@@ -14,6 +14,8 @@ import "@tiptap/extension-text-align";
14
14
  import "@tiptap/extension-underline";
15
15
  import "@tiptap/starter-kit";
16
16
  import "../extensions/CodeSnippet.js";
17
+ import "../utils/handleMarkdownPaste.js";
18
+ import "../utils/markdownToHtml.js";
17
19
  import { normalizeUrl } from "../utils/normalizeUrl.js";
18
20
  const ImagePopover = ({
19
21
  editor,
@@ -14,6 +14,8 @@ import "@tiptap/extension-text-align";
14
14
  import "@tiptap/extension-underline";
15
15
  import "@tiptap/starter-kit";
16
16
  import "../extensions/CodeSnippet.js";
17
+ import "../utils/handleMarkdownPaste.js";
18
+ import "../utils/markdownToHtml.js";
17
19
  import { normalizeUrl } from "../utils/normalizeUrl.js";
18
20
  const LinkPopover = ({
19
21
  editor,
@@ -1,7 +1,6 @@
1
1
  import { jsxs, jsx, Fragment } from "react/jsx-runtime";
2
2
  import { useState, useRef, useCallback, useLayoutEffect, useEffect } from "react";
3
3
  import Button from "@synerise/ds-button";
4
- import { useTheme } from "@synerise/ds-core";
5
4
  import Icon, { BoldM, ItalicM, StrikeM, UnderlineM, ListM, ListNumbersM, LinkM, CodeM, Grid2M, ImageM, OptionHorizontalM, AiStarM, TextM, AlignRightM, AlignCenterM, AlignLeftM, H3M, H2M, H1M, TableL, AddM, DeleteM, TrashM } from "@synerise/ds-icon";
6
5
  import Popover, { PopoverTrigger, PopoverContent } from "@synerise/ds-popover";
7
6
  import DSToolbar, { ToolbarButton, ToolbarGroup } from "@synerise/ds-toolbar";
@@ -86,7 +85,6 @@ const Toolbar = ({
86
85
  onEditWithAI,
87
86
  editWithAIOptions
88
87
  }) => {
89
- const theme = useTheme();
90
88
  const [showLinkPopover, setShowLinkPopover] = useState(false);
91
89
  const [showImagePopover, setShowImagePopover] = useState(false);
92
90
  const [showBlockMenu, setShowBlockMenu] = useState(false);
@@ -413,7 +411,7 @@ const Toolbar = ({
413
411
  (onEditWithAI || hasAiOptions) && /* @__PURE__ */ jsx(ToolbarGroup, { isCompact: true, style: {
414
412
  marginLeft: "auto"
415
413
  }, children: (() => {
416
- const aiButton = /* @__PURE__ */ jsx(Button, { type: "custom-color-ghost", color: "pink", mode: "icon-label", loading: aiLoading, disabled, icon: /* @__PURE__ */ jsx(Icon, { color: theme.palette["mars-400"], component: /* @__PURE__ */ jsx(AiStarM, {}) }), onClick: hasAiOptions ? () => setShowAiMenu((prev) => !prev) : handleEditWithAI, children: /* @__PURE__ */ jsx(AiGradientLabel, { children: texts.editWithAI }) });
414
+ const aiButton = /* @__PURE__ */ jsx(Button, { type: "custom-color-ghost", color: "pink", mode: "icon-label", loading: aiLoading, disabled, icon: /* @__PURE__ */ jsx(Icon, { color: "var(--ds-rich-text-ai-icon)", component: /* @__PURE__ */ jsx(AiStarM, {}) }), onClick: hasAiOptions ? () => setShowAiMenu((prev) => !prev) : handleEditWithAI, children: /* @__PURE__ */ jsx(AiGradientLabel, { children: texts.editWithAI }) });
417
415
  if (!hasAiOptions) {
418
416
  return aiButton;
419
417
  }
package/dist/index.js CHANGED
@@ -18,6 +18,8 @@ import "@tiptap/extension-text-align";
18
18
  import "@tiptap/extension-underline";
19
19
  import "@tiptap/starter-kit";
20
20
  import "./extensions/CodeSnippet.js";
21
+ import "./utils/handleMarkdownPaste.js";
22
+ import "./utils/markdownToHtml.js";
21
23
  import { normalizeUrl } from "./utils/normalizeUrl.js";
22
24
  import { sanitizeUrl } from "./utils/sanitizeUrl.js";
23
25
  export {
@@ -0,0 +1,2 @@
1
+ import { EditorView } from '@tiptap/pm/view';
2
+ export declare const handleMarkdownPaste: (view: EditorView, event: ClipboardEvent) => boolean;
@@ -0,0 +1,46 @@
1
+ import { DOMParser, Slice } from "@tiptap/pm/model";
2
+ import MarkdownIt from "markdown-it";
3
+ import { markdownToHtml } from "./markdownToHtml.js";
4
+ const PROSEMIRROR_SLICE_ATTRIBUTE = "data-pm-slice";
5
+ const STRUCTURED_HTML_PATTERN = /<(h[1-6]|ul|ol|table|blockquote|pre|a|strong|b|em|i|u|s|code)[\s>]/i;
6
+ const isInsideCode = ({
7
+ state
8
+ }) => state.selection.$from.parent.type.spec.code || state.selection.$to.parent.type.spec.code;
9
+ const {
10
+ linkify
11
+ } = new MarkdownIt();
12
+ const isUrlOverSelection = ({
13
+ state
14
+ }, text) => {
15
+ const links = linkify.match(text.trim());
16
+ return !state.selection.empty && links?.length === 1 && links[0].raw === text.trim();
17
+ };
18
+ const parseWithSchema = (view, markdown) => {
19
+ const {
20
+ body
21
+ } = new window.DOMParser().parseFromString(markdownToHtml(markdown, {
22
+ allowHtml: false
23
+ }), "text/html");
24
+ const {
25
+ content
26
+ } = DOMParser.fromSchema(view.state.schema).parse(body);
27
+ const openStart = content.firstChild?.type.name === "paragraph" ? 1 : 0;
28
+ const openEnd = content.lastChild?.type.name === "paragraph" ? 1 : 0;
29
+ return new Slice(content, openStart, openEnd);
30
+ };
31
+ const handleMarkdownPaste = (view, event) => {
32
+ const text = event.clipboardData?.getData("text/plain");
33
+ const html = event.clipboardData?.getData("text/html") ?? "";
34
+ if (!text?.trim() || html.includes(PROSEMIRROR_SLICE_ATTRIBUTE) || STRUCTURED_HTML_PATTERN.test(html) || isInsideCode(view) || isUrlOverSelection(view, text)) {
35
+ return false;
36
+ }
37
+ const slice = parseWithSchema(view, text);
38
+ if (!slice.content.size) {
39
+ return false;
40
+ }
41
+ view.dispatch(view.state.tr.replaceSelection(slice).scrollIntoView().setMeta("paste", true));
42
+ return true;
43
+ };
44
+ export {
45
+ handleMarkdownPaste
46
+ };
@@ -1,3 +1,5 @@
1
1
  export { getExtensions } from './getExtensions';
2
+ export { handleMarkdownPaste } from './handleMarkdownPaste';
3
+ export { markdownToHtml } from './markdownToHtml';
2
4
  export { normalizeUrl } from './normalizeUrl';
3
5
  export { sanitizeUrl } from './sanitizeUrl';
@@ -1,8 +1,12 @@
1
1
  import { getExtensions } from "./getExtensions.js";
2
+ import { handleMarkdownPaste } from "./handleMarkdownPaste.js";
3
+ import { markdownToHtml } from "./markdownToHtml.js";
2
4
  import { normalizeUrl } from "./normalizeUrl.js";
3
5
  import { sanitizeUrl } from "./sanitizeUrl.js";
4
6
  export {
5
7
  getExtensions,
8
+ handleMarkdownPaste,
9
+ markdownToHtml,
6
10
  normalizeUrl,
7
11
  sanitizeUrl
8
12
  };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Converts markdown to HTML for the editor to read, both when a value loads and when text is pasted.
3
+ * Pass `allowHtml: false` for pasted text, so tags in it stay text.
4
+ */
5
+ export declare const markdownToHtml: (value: string, { allowHtml }?: {
6
+ allowHtml?: boolean;
7
+ }) => string;
@@ -0,0 +1,17 @@
1
+ import MarkdownIt from "markdown-it";
2
+ const savedValueParser = new MarkdownIt({
3
+ html: true,
4
+ linkify: true,
5
+ breaks: false
6
+ });
7
+ const pastedTextParser = new MarkdownIt({
8
+ html: false,
9
+ linkify: true,
10
+ breaks: true
11
+ });
12
+ const markdownToHtml = (value, {
13
+ allowHtml = true
14
+ } = {}) => (allowHtml ? savedValueParser : pastedTextParser).render(value).replace(/\n<\/code><\/pre>/g, "</code></pre>");
15
+ export {
16
+ markdownToHtml
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synerise/ds-rich-text",
3
- "version": "2.0.11",
3
+ "version": "2.2.0",
4
4
  "description": "RichText UI Component for the Synerise Design System",
5
5
  "license": "ISC",
6
6
  "repository": "Synerise/synerise-design",
@@ -38,15 +38,15 @@
38
38
  "./dist/*": "./dist/*.js"
39
39
  },
40
40
  "dependencies": {
41
- "@synerise/ds-button": "^2.1.5",
42
- "@synerise/ds-copy-icon": "^2.0.10",
43
- "@synerise/ds-form-field": "^2.0.10",
44
- "@synerise/ds-icon": "^2.2.3",
45
- "@synerise/ds-input": "^2.0.11",
46
- "@synerise/ds-popover": "^2.0.4",
47
- "@synerise/ds-toolbar": "^2.0.10",
48
- "@synerise/ds-tooltip": "^2.0.10",
49
- "@synerise/ds-utils": "^2.2.2",
41
+ "@synerise/ds-button": "^2.2.0",
42
+ "@synerise/ds-copy-icon": "^2.1.0",
43
+ "@synerise/ds-form-field": "^2.1.0",
44
+ "@synerise/ds-icon": "^2.3.0",
45
+ "@synerise/ds-input": "^2.1.0",
46
+ "@synerise/ds-popover": "^2.2.0",
47
+ "@synerise/ds-toolbar": "^2.1.0",
48
+ "@synerise/ds-tooltip": "^2.1.0",
49
+ "@synerise/ds-utils": "^2.3.0",
50
50
  "@tiptap/core": "^2.11.0",
51
51
  "@tiptap/extension-image": "^2.11.0",
52
52
  "@tiptap/extension-link": "^2.11.0",
@@ -71,5 +71,5 @@
71
71
  "@types/markdown-it": "14.1.2",
72
72
  "vitest": "4"
73
73
  },
74
- "gitHead": "5ac0f7dcb7c3995b402c86c087d8f32770f80a1d"
74
+ "gitHead": "5c3d61e30289486d14c7d6becb682e2d4a08ce41"
75
75
  }