@gild-ui/react-markdown 0.0.0 → 0.1.0-alpha.1

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.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +179 -2
  3. package/dist/CodeBlock-BFqv_nVv.mjs +134 -0
  4. package/dist/CodeBlock-BaMe7CiF.cjs +151 -0
  5. package/dist/CodeBlock.cjs +5 -0
  6. package/dist/CodeBlock.d.cts +15 -0
  7. package/dist/CodeBlock.d.mts +3 -0
  8. package/dist/CodeBlock.mjs +1 -0
  9. package/dist/Control.cjs +60 -0
  10. package/dist/Control.d.cts +16 -0
  11. package/dist/Control.d.mts +16 -0
  12. package/dist/Control.mjs +59 -0
  13. package/dist/Editor.cjs +131 -0
  14. package/dist/Editor.d.cts +48 -0
  15. package/dist/Editor.d.mts +48 -0
  16. package/dist/Editor.mjs +130 -0
  17. package/dist/LiveEdit-CXw-vQpD.cjs +224 -0
  18. package/dist/LiveEdit-CzJcNsEo.mjs +215 -0
  19. package/dist/LiveEdit.cjs +3 -0
  20. package/dist/LiveEdit.d.cts +29 -0
  21. package/dist/LiveEdit.d.mts +29 -0
  22. package/dist/LiveEdit.mjs +1 -0
  23. package/dist/MarkdownContext-D28Czdll.mjs +10 -0
  24. package/dist/MarkdownContext-DS1X9B6N.cjs +21 -0
  25. package/dist/Mermaid-DR4XzGaY.mjs +248 -0
  26. package/dist/Mermaid-d1a5LqcK.cjs +253 -0
  27. package/dist/Mermaid.cjs +3 -0
  28. package/dist/Mermaid.d.cts +2 -0
  29. package/dist/Mermaid.d.mts +2 -0
  30. package/dist/Mermaid.mjs +1 -0
  31. package/dist/Preview-CIbu-nRm.cjs +268 -0
  32. package/dist/Preview-DQOQ1NKJ.mjs +258 -0
  33. package/dist/Preview.cjs +3 -0
  34. package/dist/Preview.d.cts +56 -0
  35. package/dist/Preview.d.mts +2 -0
  36. package/dist/Preview.mjs +1 -0
  37. package/dist/Toolbar-Eg7KmYbK.mjs +603 -0
  38. package/dist/Toolbar-gk3KJhCE.cjs +614 -0
  39. package/dist/Toolbar.cjs +4 -0
  40. package/dist/Toolbar.d.cts +18 -0
  41. package/dist/Toolbar.d.mts +18 -0
  42. package/dist/Toolbar.mjs +1 -0
  43. package/dist/applyAction-B3AwrWKV.cjs +121 -0
  44. package/dist/applyAction-CkTQJ5Iy.mjs +116 -0
  45. package/dist/index-BBkHCbE7.d.mts +15 -0
  46. package/dist/index-BVYb370O.d.cts +24 -0
  47. package/dist/index-CfPKWPpM.d.mts +57 -0
  48. package/dist/index-CyddYU7P.d.mts +24 -0
  49. package/dist/index.cjs +21 -0
  50. package/dist/index.d.cts +80 -0
  51. package/dist/index.d.mts +80 -0
  52. package/dist/index.mjs +9 -0
  53. package/dist/mergeOverrides-D2btS1Jb.cjs +19 -0
  54. package/dist/mergeOverrides-DEbU7yDg.mjs +14 -0
  55. package/dist/rolldown-runtime-CmQyd0Gu.cjs +27 -0
  56. package/dist/themeTypes-C4kTtkIA.d.cts +83 -0
  57. package/dist/themeTypes-C4kTtkIA.d.mts +83 -0
  58. package/package.json +166 -3
@@ -0,0 +1,131 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_applyAction = require("./applyAction-B3AwrWKV.cjs");
3
+ const require_mergeOverrides = require("./mergeOverrides-D2btS1Jb.cjs");
4
+ const require_MarkdownContext = require("./MarkdownContext-DS1X9B6N.cjs");
5
+ let react = require("react");
6
+ let react_jsx_runtime = require("react/jsx-runtime");
7
+ let _gild_ui_react_Flex = require("@gild-ui/react/Flex");
8
+ let _gild_ui_react_Typography = require("@gild-ui/react/Typography");
9
+ let _gild_ui_react = require("@gild-ui/react");
10
+ let _gild_ui_core_theme = require("@gild-ui/core/theme");
11
+ let _gild_ui_react_TextInput = require("@gild-ui/react/TextInput");
12
+ // src/Editor/Editor.tsx
13
+ const EditorRoot = (0, _gild_ui_react.styled)(_gild_ui_react_Flex.Flex, {
14
+ name: "MarkdownEditor",
15
+ label: "MarkdownEditor"
16
+ })();
17
+ const INDENT_ACTION = {
18
+ id: "indent",
19
+ label: "",
20
+ ariaLabel: "",
21
+ kind: "linePrefix",
22
+ linePrefix: " "
23
+ };
24
+ const noop = () => {};
25
+ /**
26
+ * The source `<textarea>`, with selection tracking and Tab-to-indent. Inside a `Control` it is
27
+ * driven by context (so the `Toolbar` can act on it); on its own it is a plain controlled field
28
+ * via `value`/`onChange`.
29
+ */
30
+ function Editor({ value: valueProp, onChange: onChangeProp, placeholder = "Write your article in Markdown...", minRows = 16, maxRows, variant, color, textColor: textColorProp, size, name, id, error, required, disabled, showShortcutHint = true, style, slotProps }) {
31
+ const context = (0, react.useContext)(require_MarkdownContext.MarkdownContext);
32
+ const theme = (0, _gild_ui_react.useTheme)();
33
+ const textColor = textColorProp ?? (0, _gild_ui_core_theme.themeDefault)(theme, "textColor", "initial");
34
+ const hintId = (0, react.useId)();
35
+ const localPendingRef = (0, react.useRef)(null);
36
+ const value = context?.value ?? valueProp ?? "";
37
+ const onChange = context?.onChange ?? onChangeProp ?? noop;
38
+ const paneRef = (0, react.useRef)(null);
39
+ const queueSelection = (selection) => {
40
+ if (context) context.queueSelection(selection);
41
+ else localPendingRef.current = selection;
42
+ };
43
+ const takeQueuedSelection = () => {
44
+ if (context) return context.takeQueuedSelection();
45
+ const selection = localPendingRef.current;
46
+ localPendingRef.current = null;
47
+ return selection;
48
+ };
49
+ const rememberSelection = (event) => {
50
+ if (!context) return;
51
+ const { selectionStart, selectionEnd } = event.currentTarget;
52
+ context.rememberSelection({
53
+ start: selectionStart,
54
+ end: selectionEnd
55
+ });
56
+ };
57
+ const handleKeyDown = (event) => {
58
+ const el = event.currentTarget;
59
+ if (event.key === "Escape" || (event.metaKey || event.ctrlKey) && event.shiftKey && event.key.toLowerCase() === "m") {
60
+ event.preventDefault();
61
+ el.blur();
62
+ return;
63
+ }
64
+ if (event.key === "Tab" && !event.shiftKey) {
65
+ event.preventDefault();
66
+ const { selectionStart, selectionEnd } = el;
67
+ const next = value.slice(selectionStart, selectionEnd).includes("\n") ? require_applyAction.applyAction(INDENT_ACTION, {
68
+ value,
69
+ selectionStart,
70
+ selectionEnd
71
+ }) : {
72
+ value: value.slice(0, selectionStart) + " " + value.slice(selectionEnd),
73
+ selectionStart: selectionStart + 1,
74
+ selectionEnd: selectionStart + 1
75
+ };
76
+ queueSelection({
77
+ start: next.selectionStart,
78
+ end: next.selectionEnd
79
+ });
80
+ onChange(next.value);
81
+ }
82
+ };
83
+ (0, react.useLayoutEffect)(() => {
84
+ const selection = takeQueuedSelection();
85
+ if (!selection) return;
86
+ const textarea = paneRef.current.querySelector("textarea");
87
+ textarea.focus();
88
+ textarea.setSelectionRange(selection.start, selection.end);
89
+ });
90
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(EditorRoot, {
91
+ ref: paneRef,
92
+ flexDirection: "column",
93
+ flex: 1,
94
+ minWidth: 0,
95
+ gap: 1,
96
+ ...require_mergeOverrides.mergeOverrides(slotProps?.root, { style }),
97
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_gild_ui_react_TextInput.TextInput, {
98
+ multiline: true,
99
+ resize: true,
100
+ fullWidth: true,
101
+ variant,
102
+ color,
103
+ textColor,
104
+ size,
105
+ name,
106
+ id,
107
+ error,
108
+ required,
109
+ disabled,
110
+ minRows,
111
+ maxRows,
112
+ value,
113
+ placeholder,
114
+ onChange: (event) => onChange(event.target.value),
115
+ inputProps: {
116
+ "aria-label": "Markdown source",
117
+ "aria-describedby": showShortcutHint ? hintId : void 0,
118
+ onSelect: rememberSelection,
119
+ onBlur: rememberSelection,
120
+ onKeyDown: handleKeyDown
121
+ }
122
+ }), showShortcutHint && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_gild_ui_react_Typography.Typography, {
123
+ id: hintId,
124
+ variant: "caption",
125
+ color: "secondary",
126
+ m: 0,
127
+ children: "Tab inserts a tab · press Escape or Ctrl/Cmd+Shift+M to move focus out."
128
+ })]
129
+ });
130
+ }
131
+ exports.Editor = Editor;
@@ -0,0 +1,48 @@
1
+ import "./themeTypes-C4kTtkIA.cjs";
2
+ import { SlotProps, StyleOverride } from "@gild-ui/react";
3
+ import { TextInputColor, TextInputSize, TextInputTextColor, TextInputVariant } from "@gild-ui/react/TextInput";
4
+ export interface EditorProps {
5
+ /** Standalone source value - used when rendered outside a `Control`. Ignored inside one. */
6
+ value?: string;
7
+ /** Standalone change handler - used when rendered outside a `Control`. Ignored inside one. */
8
+ onChange?: (value: string) => void;
9
+ /** Placeholder shown while the source is empty. */
10
+ placeholder?: string;
11
+ /** Minimum visible rows of the source textarea. Default: `16`. */
12
+ minRows?: number;
13
+ /**
14
+ * Maximum rows before the auto-grow textarea stops growing and scrolls
15
+ * internally - keeps its border box visible inside height-bound panes.
16
+ */
17
+ maxRows?: number;
18
+ /** Visual style of the source input - forwarded to `TextInput`. Default: `'default'`. */
19
+ variant?: TextInputVariant;
20
+ /** Focus/active border colour - forwarded to `TextInput`. */
21
+ color?: TextInputColor;
22
+ /** Text colour of the typed source - forwarded to `TextInput`. Default: `'initial'`. */
23
+ textColor?: TextInputTextColor;
24
+ /** Padding/font-size density - forwarded to `TextInput`. */
25
+ size?: TextInputSize;
26
+ /** Native `name` on the textarea - set it for native `<form>` submission / FormData. */
27
+ name?: string;
28
+ /** `id` for label association. Falls back to the enclosing `FormControl`. */
29
+ id?: string;
30
+ /** Marks the field invalid (error border). Falls back to the enclosing `FormControl`. */
31
+ error?: boolean;
32
+ /** Marks the field required. Falls back to the enclosing `FormControl`. */
33
+ required?: boolean;
34
+ /** Disables editing. Falls back to the enclosing `FormControl`. */
35
+ disabled?: boolean;
36
+ /** Shows a one-line hint about the Tab / focus-release shortcuts under the field. Default: `true`. */
37
+ showShortcutHint?: boolean;
38
+ /** Overrides the CSS of the root: an entry of `StyleSheet.create`, or a list of them. It wins over `slotProps.root.style`. */
39
+ style?: StyleOverride;
40
+ /** Overrides for the slots a theme can style: `style`, `className`, `data-*` and `aria-*`. */
41
+ slotProps?: SlotProps<'root'>;
42
+ }
43
+ /**
44
+ * The source `<textarea>`, with selection tracking and Tab-to-indent. Inside a `Control` it is
45
+ * driven by context (so the `Toolbar` can act on it); on its own it is a plain controlled field
46
+ * via `value`/`onChange`.
47
+ */
48
+ export declare function Editor({ value: valueProp, onChange: onChangeProp, placeholder, minRows, maxRows, variant, color, textColor: textColorProp, size, name, id, error, required, disabled, showShortcutHint, style, slotProps }: Readonly<EditorProps>): import("react").JSX.Element;
@@ -0,0 +1,48 @@
1
+ import "./themeTypes-C4kTtkIA.mjs";
2
+ import { SlotProps, StyleOverride } from "@gild-ui/react";
3
+ import { TextInputColor, TextInputSize, TextInputTextColor, TextInputVariant } from "@gild-ui/react/TextInput";
4
+ export interface EditorProps {
5
+ /** Standalone source value - used when rendered outside a `Control`. Ignored inside one. */
6
+ value?: string;
7
+ /** Standalone change handler - used when rendered outside a `Control`. Ignored inside one. */
8
+ onChange?: (value: string) => void;
9
+ /** Placeholder shown while the source is empty. */
10
+ placeholder?: string;
11
+ /** Minimum visible rows of the source textarea. Default: `16`. */
12
+ minRows?: number;
13
+ /**
14
+ * Maximum rows before the auto-grow textarea stops growing and scrolls
15
+ * internally - keeps its border box visible inside height-bound panes.
16
+ */
17
+ maxRows?: number;
18
+ /** Visual style of the source input - forwarded to `TextInput`. Default: `'default'`. */
19
+ variant?: TextInputVariant;
20
+ /** Focus/active border colour - forwarded to `TextInput`. */
21
+ color?: TextInputColor;
22
+ /** Text colour of the typed source - forwarded to `TextInput`. Default: `'initial'`. */
23
+ textColor?: TextInputTextColor;
24
+ /** Padding/font-size density - forwarded to `TextInput`. */
25
+ size?: TextInputSize;
26
+ /** Native `name` on the textarea - set it for native `<form>` submission / FormData. */
27
+ name?: string;
28
+ /** `id` for label association. Falls back to the enclosing `FormControl`. */
29
+ id?: string;
30
+ /** Marks the field invalid (error border). Falls back to the enclosing `FormControl`. */
31
+ error?: boolean;
32
+ /** Marks the field required. Falls back to the enclosing `FormControl`. */
33
+ required?: boolean;
34
+ /** Disables editing. Falls back to the enclosing `FormControl`. */
35
+ disabled?: boolean;
36
+ /** Shows a one-line hint about the Tab / focus-release shortcuts under the field. Default: `true`. */
37
+ showShortcutHint?: boolean;
38
+ /** Overrides the CSS of the root: an entry of `StyleSheet.create`, or a list of them. It wins over `slotProps.root.style`. */
39
+ style?: StyleOverride;
40
+ /** Overrides for the slots a theme can style: `style`, `className`, `data-*` and `aria-*`. */
41
+ slotProps?: SlotProps<'root'>;
42
+ }
43
+ /**
44
+ * The source `<textarea>`, with selection tracking and Tab-to-indent. Inside a `Control` it is
45
+ * driven by context (so the `Toolbar` can act on it); on its own it is a plain controlled field
46
+ * via `value`/`onChange`.
47
+ */
48
+ export declare function Editor({ value: valueProp, onChange: onChangeProp, placeholder, minRows, maxRows, variant, color, textColor: textColorProp, size, name, id, error, required, disabled, showShortcutHint, style, slotProps }: Readonly<EditorProps>): import("react").JSX.Element;
@@ -0,0 +1,130 @@
1
+ import { t as applyAction } from "./applyAction-CkTQJ5Iy.mjs";
2
+ import { t as mergeOverrides } from "./mergeOverrides-DEbU7yDg.mjs";
3
+ import { t as MarkdownContext } from "./MarkdownContext-D28Czdll.mjs";
4
+ import { useContext, useId, useLayoutEffect, useRef } from "react";
5
+ import { jsx, jsxs } from "react/jsx-runtime";
6
+ import { Flex } from "@gild-ui/react/Flex";
7
+ import { Typography } from "@gild-ui/react/Typography";
8
+ import { styled, useTheme } from "@gild-ui/react";
9
+ import { themeDefault } from "@gild-ui/core/theme";
10
+ import { TextInput } from "@gild-ui/react/TextInput";
11
+ // src/Editor/Editor.tsx
12
+ const EditorRoot = styled(Flex, {
13
+ name: "MarkdownEditor",
14
+ label: "MarkdownEditor"
15
+ })();
16
+ const INDENT_ACTION = {
17
+ id: "indent",
18
+ label: "",
19
+ ariaLabel: "",
20
+ kind: "linePrefix",
21
+ linePrefix: " "
22
+ };
23
+ const noop = () => {};
24
+ /**
25
+ * The source `<textarea>`, with selection tracking and Tab-to-indent. Inside a `Control` it is
26
+ * driven by context (so the `Toolbar` can act on it); on its own it is a plain controlled field
27
+ * via `value`/`onChange`.
28
+ */
29
+ function Editor({ value: valueProp, onChange: onChangeProp, placeholder = "Write your article in Markdown...", minRows = 16, maxRows, variant, color, textColor: textColorProp, size, name, id, error, required, disabled, showShortcutHint = true, style, slotProps }) {
30
+ const context = useContext(MarkdownContext);
31
+ const theme = useTheme();
32
+ const textColor = textColorProp ?? themeDefault(theme, "textColor", "initial");
33
+ const hintId = useId();
34
+ const localPendingRef = useRef(null);
35
+ const value = context?.value ?? valueProp ?? "";
36
+ const onChange = context?.onChange ?? onChangeProp ?? noop;
37
+ const paneRef = useRef(null);
38
+ const queueSelection = (selection) => {
39
+ if (context) context.queueSelection(selection);
40
+ else localPendingRef.current = selection;
41
+ };
42
+ const takeQueuedSelection = () => {
43
+ if (context) return context.takeQueuedSelection();
44
+ const selection = localPendingRef.current;
45
+ localPendingRef.current = null;
46
+ return selection;
47
+ };
48
+ const rememberSelection = (event) => {
49
+ if (!context) return;
50
+ const { selectionStart, selectionEnd } = event.currentTarget;
51
+ context.rememberSelection({
52
+ start: selectionStart,
53
+ end: selectionEnd
54
+ });
55
+ };
56
+ const handleKeyDown = (event) => {
57
+ const el = event.currentTarget;
58
+ if (event.key === "Escape" || (event.metaKey || event.ctrlKey) && event.shiftKey && event.key.toLowerCase() === "m") {
59
+ event.preventDefault();
60
+ el.blur();
61
+ return;
62
+ }
63
+ if (event.key === "Tab" && !event.shiftKey) {
64
+ event.preventDefault();
65
+ const { selectionStart, selectionEnd } = el;
66
+ const next = value.slice(selectionStart, selectionEnd).includes("\n") ? applyAction(INDENT_ACTION, {
67
+ value,
68
+ selectionStart,
69
+ selectionEnd
70
+ }) : {
71
+ value: value.slice(0, selectionStart) + " " + value.slice(selectionEnd),
72
+ selectionStart: selectionStart + 1,
73
+ selectionEnd: selectionStart + 1
74
+ };
75
+ queueSelection({
76
+ start: next.selectionStart,
77
+ end: next.selectionEnd
78
+ });
79
+ onChange(next.value);
80
+ }
81
+ };
82
+ useLayoutEffect(() => {
83
+ const selection = takeQueuedSelection();
84
+ if (!selection) return;
85
+ const textarea = paneRef.current.querySelector("textarea");
86
+ textarea.focus();
87
+ textarea.setSelectionRange(selection.start, selection.end);
88
+ });
89
+ return /* @__PURE__ */ jsxs(EditorRoot, {
90
+ ref: paneRef,
91
+ flexDirection: "column",
92
+ flex: 1,
93
+ minWidth: 0,
94
+ gap: 1,
95
+ ...mergeOverrides(slotProps?.root, { style }),
96
+ children: [/* @__PURE__ */ jsx(TextInput, {
97
+ multiline: true,
98
+ resize: true,
99
+ fullWidth: true,
100
+ variant,
101
+ color,
102
+ textColor,
103
+ size,
104
+ name,
105
+ id,
106
+ error,
107
+ required,
108
+ disabled,
109
+ minRows,
110
+ maxRows,
111
+ value,
112
+ placeholder,
113
+ onChange: (event) => onChange(event.target.value),
114
+ inputProps: {
115
+ "aria-label": "Markdown source",
116
+ "aria-describedby": showShortcutHint ? hintId : void 0,
117
+ onSelect: rememberSelection,
118
+ onBlur: rememberSelection,
119
+ onKeyDown: handleKeyDown
120
+ }
121
+ }), showShortcutHint && /* @__PURE__ */ jsx(Typography, {
122
+ id: hintId,
123
+ variant: "caption",
124
+ color: "secondary",
125
+ m: 0,
126
+ children: "Tab inserts a tab · press Escape or Ctrl/Cmd+Shift+M to move focus out."
127
+ })]
128
+ });
129
+ }
130
+ export { Editor };
@@ -0,0 +1,224 @@
1
+ const require_rolldown_runtime = require("./rolldown-runtime-CmQyd0Gu.cjs");
2
+ const require_MarkdownContext = require("./MarkdownContext-DS1X9B6N.cjs");
3
+ const require_Preview = require("./Preview-CIbu-nRm.cjs");
4
+ const require_mergeOverrides = require("./mergeOverrides-D2btS1Jb.cjs");
5
+ let react = require("react");
6
+ let react_jsx_runtime = require("react/jsx-runtime");
7
+ let _gild_ui_react = require("@gild-ui/react");
8
+ let _gild_ui_react_View = require("@gild-ui/react/View");
9
+ let remark_gfm = require("remark-gfm");
10
+ remark_gfm = require_rolldown_runtime.__toESM(remark_gfm, 1);
11
+ let rehype_parse = require("rehype-parse");
12
+ rehype_parse = require_rolldown_runtime.__toESM(rehype_parse, 1);
13
+ let rehype_remark = require("rehype-remark");
14
+ rehype_remark = require_rolldown_runtime.__toESM(rehype_remark, 1);
15
+ let remark_stringify = require("remark-stringify");
16
+ remark_stringify = require_rolldown_runtime.__toESM(remark_stringify, 1);
17
+ // src/utils/blockToMarkdown.ts
18
+ const processor = (0, require("unified").unified)().use(rehype_parse.default, { fragment: true }).use(rehype_remark.default).use(remark_gfm.default).use(remark_stringify.default, {
19
+ bullet: "-",
20
+ fence: "`",
21
+ rule: "-"
22
+ });
23
+ /**
24
+ * Serializes an edited block's live DOM back to markdown. The preview renders
25
+ * non-content UI inside a block (the code-copy control, decorative SVGs) -
26
+ * those are stripped from a clone first so only real content round-trips.
27
+ */
28
+ function blockToMarkdown(element) {
29
+ const clone = element.cloneNode(true);
30
+ for (const junk of clone.querySelectorAll(".code-copy, button, svg")) junk.remove();
31
+ return String(processor.processSync(clone.innerHTML)).trimEnd();
32
+ }
33
+ // src/utils/splitBlocks.ts
34
+ const FENCE = /^[ \t]{0,3}(`{3,}|~{3,})/;
35
+ /**
36
+ * The fence state after `line`: unchanged when the line carries no marker, the
37
+ * marker itself when one opens a block, and `null` when one closes it.
38
+ *
39
+ * A closing marker must use the same character, be at least as long, and carry
40
+ * nothing after it. The info string is what separates the two roles - a
41
+ * ```` ```ts ```` line inside an open ```` ``` ```` block opens nothing and closes
42
+ * nothing, it is content. Without that check the block would end early and its
43
+ * remaining blank lines would split it into further blocks.
44
+ */
45
+ const nextFence = (line, fence) => {
46
+ const match = FENCE.exec(line);
47
+ if (!match) return fence;
48
+ const [matched, marker] = match;
49
+ if (fence === null) return marker;
50
+ return line.slice(matched.length).trim() === "" && marker.startsWith(fence[0]) && marker.length >= fence.length ? null : fence;
51
+ };
52
+ /**
53
+ * Splits markdown into top-level blocks separated by blank lines, keeping
54
+ * fenced code blocks whole - blank lines inside a ``` / ~~~ fence never split.
55
+ * Offsets index the original string, so an edited block's source can be
56
+ * spliced back losslessly.
57
+ */
58
+ function splitBlocks(value) {
59
+ const blocks = [];
60
+ let blockStart = -1;
61
+ let blockEnd = -1;
62
+ let fence = null;
63
+ let offset = 0;
64
+ for (const line of value.split("\n")) {
65
+ const isBlank = line.trim() === "";
66
+ if (blockStart >= 0 && isBlank && fence === null) {
67
+ blocks.push({
68
+ source: value.slice(blockStart, blockEnd),
69
+ start: blockStart,
70
+ end: blockEnd
71
+ });
72
+ blockStart = -1;
73
+ } else if (!isBlank || fence !== null) {
74
+ if (blockStart < 0) blockStart = offset;
75
+ blockEnd = offset + line.length;
76
+ fence = nextFence(line, fence);
77
+ }
78
+ offset += line.length + 1;
79
+ }
80
+ if (blockStart >= 0) blocks.push({
81
+ source: value.slice(blockStart, blockEnd),
82
+ start: blockStart,
83
+ end: blockEnd
84
+ });
85
+ return blocks;
86
+ }
87
+ // src/LiveEdit/LiveEdit.tsx
88
+ const LiveEditRoot = (0, _gild_ui_react.styled)(_gild_ui_react_View.View, {
89
+ name: "MarkdownLiveEdit",
90
+ label: "MarkdownLiveEdit"
91
+ })({
92
+ minWidth: 0,
93
+ maxWidth: "100%",
94
+ overflowWrap: "break-word"
95
+ });
96
+ const LiveEditBlock = (0, _gild_ui_react.styled)(_gild_ui_react_View.View, {
97
+ name: "MarkdownLiveEdit",
98
+ slot: "block",
99
+ label: "MarkdownLiveEditBlock"
100
+ })(({ theme }) => ({
101
+ cursor: "text",
102
+ borderRadius: theme?.radii?.sm,
103
+ "&:hover, &:focus-within, &:focus": {
104
+ outline: `${theme?.borderWidths?.thin} dashed ${theme?.border?.primary}`,
105
+ outlineOffset: "2px"
106
+ },
107
+ "&:empty::before": {
108
+ content: "attr(data-placeholder)",
109
+ color: theme?.text?.secondary
110
+ }
111
+ }));
112
+ const MERMAID_FENCE = /^\s{0,3}(?:`{3,}|~{3,})\s*mermaid\b/;
113
+ const noop = () => {};
114
+ /** An empty document still needs one editable block to type into. */
115
+ const toBlocks = (value) => {
116
+ const blocks = splitBlocks(value);
117
+ return blocks.length > 0 ? blocks : [{
118
+ source: "",
119
+ start: 0,
120
+ end: value.length
121
+ }];
122
+ };
123
+ /**
124
+ * One contentEditable block. The preview element is created once per mount -
125
+ * while the browser owns the DOM during typing, React must never reconcile
126
+ * the subtree under the caret. The parent remounts the block (source-derived
127
+ * key) whenever its markdown actually changes.
128
+ */
129
+ function EditableBlock({ index, source, isEditable, placeholder, slotProps, blockProps, onEdit, onDone }) {
130
+ const [content] = (0, react.useState)(() => source === "" ? null : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Preview.Preview, {
131
+ slotProps,
132
+ children: source
133
+ }));
134
+ const handleKeyDown = (event) => {
135
+ if (event.key !== "Escape") return;
136
+ event.preventDefault();
137
+ event.currentTarget.blur();
138
+ };
139
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LiveEditBlock, {
140
+ ...blockProps,
141
+ "aria-label": "Edit block",
142
+ tabIndex: isEditable ? 0 : void 0,
143
+ contentEditable: isEditable,
144
+ suppressContentEditableWarning: true,
145
+ "data-placeholder": source === "" ? placeholder : void 0,
146
+ onInput: (event) => onEdit(index, event.currentTarget),
147
+ onBlur: onDone,
148
+ onKeyDown: handleKeyDown,
149
+ children: content
150
+ });
151
+ }
152
+ /**
153
+ * A preview you write on directly: the document renders exactly like
154
+ * `Preview`, split into blocks, and every block is `contentEditable` - no
155
+ * textarea. While a block is being typed in, the browser owns its DOM; each
156
+ * input serializes that block back to markdown and splices it into `value`,
157
+ * so `onChange` stays live and untouched blocks keep their exact source.
158
+ * Blur (or Escape) re-splits the document. Mermaid blocks are read-only -
159
+ * a diagram has no text to edit in place. Inside a `Control` it is driven by
160
+ * context; standalone it is a plain controlled component.
161
+ */
162
+ function LiveEdit({ value: valueProp, onChange: onChangeProp, placeholder = "Click to start writing...", slotProps, style }) {
163
+ const { root, block: blockSlot, ...elementProps } = slotProps ?? {};
164
+ const context = (0, react.useContext)(require_MarkdownContext.MarkdownContext);
165
+ const value = context?.value ?? valueProp ?? "";
166
+ const onChange = context?.onChange ?? onChangeProp ?? noop;
167
+ const [blocks, setBlocks] = (0, react.useState)(() => toBlocks(value));
168
+ const [generation, setGeneration] = (0, react.useState)(0);
169
+ const offsetsRef = (0, react.useRef)(null);
170
+ const getOffsets = () => {
171
+ if (offsetsRef.current?.generation !== generation) offsetsRef.current = {
172
+ generation,
173
+ offsets: blocks.map(({ start, end }) => ({
174
+ start,
175
+ end
176
+ }))
177
+ };
178
+ return offsetsRef.current.offsets;
179
+ };
180
+ const resplit = (next) => {
181
+ setBlocks(toBlocks(next));
182
+ setGeneration((current) => current + 1);
183
+ };
184
+ const syncedValueRef = (0, react.useRef)(value);
185
+ if (value !== syncedValueRef.current) {
186
+ syncedValueRef.current = value;
187
+ resplit(value);
188
+ }
189
+ const handleEdit = (index, element) => {
190
+ const markdown = blockToMarkdown(element);
191
+ const offsets = getOffsets();
192
+ const block = offsets[index];
193
+ const current = syncedValueRef.current;
194
+ const next = current.slice(0, block.start) + markdown + current.slice(block.end);
195
+ const delta = markdown.length - (block.end - block.start);
196
+ block.end = block.start + markdown.length;
197
+ for (const later of offsets.slice(index + 1)) {
198
+ later.start += delta;
199
+ later.end += delta;
200
+ }
201
+ syncedValueRef.current = next;
202
+ onChange(next);
203
+ };
204
+ const handleDone = () => resplit(syncedValueRef.current);
205
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LiveEditRoot, {
206
+ ...require_mergeOverrides.mergeOverrides(root, { style }),
207
+ children: blocks.map((block, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditableBlock, {
208
+ index,
209
+ source: block.source,
210
+ isEditable: !MERMAID_FENCE.test(block.source),
211
+ placeholder,
212
+ slotProps: elementProps,
213
+ blockProps: blockSlot,
214
+ onEdit: handleEdit,
215
+ onDone: handleDone
216
+ }, `${generation}:${index}`))
217
+ });
218
+ }
219
+ Object.defineProperty(exports, "LiveEdit", {
220
+ enumerable: true,
221
+ get: function() {
222
+ return LiveEdit;
223
+ }
224
+ });