@usememos/editor 26.10.1 → 26.10.2

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/dist/react.js CHANGED
@@ -1,8 +1,9 @@
1
1
  // src/react/index.tsx
2
2
  import { Editor } from "@usememos/editor";
3
3
  import { forwardRef, useEffect, useRef, useState } from "react";
4
+ import { flushSync } from "react-dom";
4
5
  import { jsx } from "react/jsx-runtime";
5
- var LIVE = ["placeholder", "editable", "attributes", "resolveImageSrc", "theme"];
6
+ var LIVE = ["placeholder", "editable", "taskToggle", "attributes", "resolveImageSrc", "theme", "extensions"];
6
7
  var extensionKey = (extensions) => (extensions ?? []).map((e) => e.name).join("\n");
7
8
  var MarkdownEditor = forwardRef(function MarkdownEditor2(props, ref) {
8
9
  const container = useRef(null);
@@ -22,7 +23,7 @@ var MarkdownEditor = forwardRef(function MarkdownEditor2(props, ref) {
22
23
  onChange: (markdown) => {
23
24
  pending.current.push(markdown);
24
25
  if (pending.current.length > 100) pending.current.shift();
25
- latest.current.onChange?.(markdown);
26
+ flushSync(() => latest.current.onChange?.(markdown));
26
27
  },
27
28
  onSubmit: (markdown) => latest.current.onSubmit?.(markdown),
28
29
  onFocus: () => latest.current.onFocus?.(),
@@ -80,7 +81,7 @@ function useLiveOptions(editorRef, props) {
80
81
  let any = false;
81
82
  for (const key of LIVE) {
82
83
  if (props[key] === applied.current.props[key]) continue;
83
- changed[key] = key === "editable" ? props.editable ?? true : props[key];
84
+ changed[key] = key === "editable" ? props.editable ?? true : key === "taskToggle" ? props.taskToggle ?? false : props[key];
84
85
  any = true;
85
86
  }
86
87
  applied.current = { editor, props: { ...props } };
@@ -91,6 +92,9 @@ function useEditor(options, deps = []) {
91
92
  const [editor, setEditor] = useState(null);
92
93
  const latest = useRef(options);
93
94
  latest.current = options;
95
+ useEffect(() => {
96
+ if (editor && options.extensions) editor.setOptions({ extensions: options.extensions });
97
+ }, [editor, options.extensions]);
94
98
  useEffect(() => {
95
99
  const o = latest.current;
96
100
  const element = document.createElement("div");
@@ -113,6 +117,19 @@ function useEditor(options, deps = []) {
113
117
  }, deps);
114
118
  return editor;
115
119
  }
120
+ function useSuggestion(editor, name) {
121
+ const select = (state2) => state2 && (name === void 0 || state2.name === name) ? state2 : null;
122
+ const [state, setState] = useState(() => select(editor?.suggestion.state));
123
+ useEffect(() => {
124
+ if (!editor) {
125
+ setState(null);
126
+ return;
127
+ }
128
+ setState(select(editor.suggestion.state));
129
+ return editor.on("suggestionChange", ({ state: state2 }) => setState(select(state2)));
130
+ }, [editor, name]);
131
+ return state;
132
+ }
116
133
  function EditorContent({ editor, className, style }) {
117
134
  const container = useRef(null);
118
135
  useEffect(() => {
@@ -138,6 +155,7 @@ function assignRef(ref, value) {
138
155
  export {
139
156
  EditorContent,
140
157
  MarkdownEditor,
141
- useEditor
158
+ useEditor,
159
+ useSuggestion
142
160
  };
143
161
  //# sourceMappingURL=react.js.map
package/dist/react.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/react/index.tsx"],
4
- "sourcesContent": ["import { Editor, type EditorOptions } from '@usememos/editor';\nimport { type CSSProperties, type ForwardedRef, forwardRef, useEffect, useRef, useState } from 'react';\n\nexport type { Editor } from '@usememos/editor';\n\n/** Constructor options that the React components take as props. */\ntype EditorProps = Pick<\n EditorOptions,\n | 'markdown'\n | 'extensions'\n | 'placeholder'\n | 'editable'\n | 'autofocus'\n | 'attributes'\n | 'resolveImageSrc'\n | 'theme'\n | 'toolbar'\n | 'slashMenu'\n | 'labels'\n>;\n\nexport interface MarkdownEditorProps extends EditorProps {\n /** Markdown. The editor updates only when this is not a value it emitted itself. */\n value?: string;\n /** Initial Markdown for uncontrolled use. */\n defaultValue?: string;\n /** Called after document changes, at most once per animation frame. */\n onChange?: (markdown: string) => void;\n /** Called on Mod+Enter or Ctrl+Enter with the current Markdown. */\n onSubmit?: (markdown: string) => void;\n onFocus?: () => void;\n onBlur?: () => void;\n className?: string;\n style?: CSSProperties;\n}\n\n/** Options that `setOptions` applies without recreating the editor. */\nconst LIVE = ['placeholder', 'editable', 'attributes', 'resolveImageSrc', 'theme'] as const;\n\n/** The extension list's identity for recreating the editor: names, so an inline array does not recreate it on every render. */\nconst extensionKey = (extensions: EditorOptions['extensions']) => (extensions ?? []).map((e) => e.name).join('\\n');\n\n/**\n * The editor as a controlled React component. A `ref` gives the core `Editor`.\n *\n * `placeholder`, `editable`, `attributes`, `resolveImageSrc` and `theme` update live.\n * A change to the names in `extensions` recreates the editor, keeping its\n * Markdown. Other props are read when the editor is created; change the\n * component's `key` to apply new values.\n *\n * A `value` that arrives while an input method is composing text is applied\n * when the composition ends, so it does not break the composition.\n */\nexport const MarkdownEditor = forwardRef(function MarkdownEditor(props: MarkdownEditorProps, ref: ForwardedRef<Editor | null>) {\n const container = useRef<HTMLDivElement>(null);\n const editorRef = useRef<Editor | null>(null);\n // Values the editor emitted that the parent has not passed back yet, oldest\n // first. While the user keeps typing, React may hand back an older one;\n // those echoes must not reset the document.\n const pending = useRef<string[]>([]);\n // An outside value that arrived during IME composition.\n const deferred = useRef<string | null>(null);\n // Callbacks are read through a ref so changing them does not recreate the editor.\n const latest = useRef(props);\n latest.current = props;\n const extensions = extensionKey(props.extensions);\n\n useEffect(() => {\n const { value, defaultValue, onChange, onSubmit, onFocus, onBlur, className, style, ...options } = latest.current;\n const previous = editorRef.current;\n const editor = new Editor({\n ...options,\n element: container.current!,\n content: previous?.getMarkdown() ?? value ?? defaultValue ?? '',\n onChange: (markdown) => {\n pending.current.push(markdown);\n if (pending.current.length > 100) pending.current.shift();\n latest.current.onChange?.(markdown);\n },\n onSubmit: (markdown) => latest.current.onSubmit?.(markdown),\n onFocus: () => latest.current.onFocus?.(),\n onBlur: () => latest.current.onBlur?.(),\n });\n editorRef.current = editor;\n pending.current = [];\n assignRef(ref, editor);\n const root = container.current!;\n const onCompositionEnd = () => {\n // Runs after the editor's own compositionend handler, which was added first.\n const value = deferred.current;\n deferred.current = null;\n if (value !== null && value !== editor.getMarkdown()) {\n pending.current = [];\n editor.setMarkdown(value);\n }\n };\n root.addEventListener('compositionend', onCompositionEnd);\n return () => {\n root.removeEventListener('compositionend', onCompositionEnd);\n editor.destroy();\n if (editorRef.current === editor) editorRef.current = null;\n assignRef(ref, null);\n };\n // Recreate only when the extension names change; see the note on the component.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [extensions]);\n\n useEffect(() => {\n const editor = editorRef.current;\n if (!editor || props.value === undefined) return;\n const echo = pending.current.indexOf(props.value);\n if (echo >= 0) {\n // The parent caught up to a value the editor emitted.\n pending.current.splice(0, echo + 1);\n return;\n }\n if (props.value === editor.getMarkdown()) return;\n if (editor.isComposing) {\n deferred.current = props.value;\n return;\n }\n pending.current = [];\n editor.setMarkdown(props.value);\n }, [props.value]);\n\n useLiveOptions(editorRef, props);\n\n return <div ref={container} className={props.className} style={props.style} />;\n});\n\n/** Applies the live options when they change. */\nfunction useLiveOptions(editorRef: { current: Editor | null }, props: EditorProps): void {\n const applied = useRef<{ editor: Editor | null; props: Partial<EditorProps> }>({ editor: null, props: {} });\n useEffect(() => {\n const editor = editorRef.current;\n if (!editor) return;\n if (applied.current.editor !== editor) {\n // A new editor was created with these props.\n applied.current = { editor, props: { ...props } };\n return;\n }\n const changed: Partial<Pick<EditorOptions, (typeof LIVE)[number]>> = {};\n let any = false;\n for (const key of LIVE) {\n if (props[key] === applied.current.props[key]) continue;\n (changed as Record<string, unknown>)[key] = key === 'editable' ? (props.editable ?? true) : props[key];\n any = true;\n }\n applied.current = { editor, props: { ...props } };\n if (any) editor.setOptions(changed);\n });\n}\n\n/**\n * Creates an editor for the lifetime of the component, or until a value in\n * `deps` changes. Returns null until the editor exists. Pair it with\n * `<EditorContent editor={editor} />`, which shows it.\n *\n * Callbacks in `options` are read when called, so they may change between\n * renders without recreating the editor.\n */\nexport function useEditor(options: Omit<EditorOptions, 'element'>, deps: readonly unknown[] = []): Editor | null {\n const [editor, setEditor] = useState<Editor | null>(null);\n const latest = useRef(options);\n latest.current = options;\n\n useEffect(() => {\n const o = latest.current;\n const element = document.createElement('div');\n const instance = new Editor({\n ...o,\n element,\n // A detached element cannot take focus; EditorContent focuses once mounted.\n autofocus: false,\n onChange: (markdown, e) => latest.current.onChange?.(markdown, e),\n onSubmit: (markdown, e) => latest.current.onSubmit?.(markdown, e),\n onFocus: (e) => latest.current.onFocus?.(e),\n onBlur: (e) => latest.current.onBlur?.(e),\n });\n if (o.autofocus) pendingAutofocus.set(instance, o.autofocus === 'end' ? 'end' : 'start');\n setEditor(instance);\n return () => {\n instance.destroy();\n setEditor(null);\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, deps);\n\n return editor;\n}\n\nexport interface EditorContentProps {\n editor: Editor | null;\n className?: string;\n style?: CSSProperties;\n}\n\n/** Shows an editor created with `useEditor`. */\nexport function EditorContent({ editor, className, style }: EditorContentProps) {\n const container = useRef<HTMLDivElement>(null);\n useEffect(() => {\n const host = container.current;\n if (!editor || !host) return;\n host.appendChild(editor.root);\n const autofocus = pendingAutofocus.get(editor);\n if (autofocus) {\n pendingAutofocus.delete(editor);\n editor.commands.focus(autofocus);\n }\n return () => {\n if (editor.root.parentNode === host) host.removeChild(editor.root);\n };\n }, [editor]);\n return <div ref={container} className={className} style={style} />;\n}\n\n/** Where to focus an editor from `useEditor` once `EditorContent` mounts it. */\nconst pendingAutofocus = new WeakMap<Editor, 'start' | 'end'>();\n\nfunction assignRef<T>(ref: ForwardedRef<T>, value: T): void {\n if (typeof ref === 'function') ref(value);\n else if (ref) ref.current = value;\n}\n"],
5
- "mappings": ";AAAA,SAAS,cAAkC;AAC3C,SAAgD,YAAY,WAAW,QAAQ,gBAAgB;AA8HtF;AA1FT,IAAM,OAAO,CAAC,eAAe,YAAY,cAAc,mBAAmB,OAAO;AAGjF,IAAM,eAAe,CAAC,gBAA6C,cAAc,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,KA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AO,KAAK,eAAe,KAAM,MAAK,YAAY,OAAO,IAAI;AAAA,IACnE;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AACX,SAAO,oBAAC,SAAI,KAAK,WAAW,WAAsB,OAAc;AAClE;AAGA,IAAM,mBAAmB,oBAAI,QAAiC;AAE9D,SAAS,UAAa,KAAsB,OAAgB;AAC1D,MAAI,OAAO,QAAQ,WAAY,KAAI,KAAK;AAAA,WAC/B,IAAK,KAAI,UAAU;AAC9B;",
6
- "names": ["MarkdownEditor", "value"]
4
+ "sourcesContent": ["import { Editor, type EditorOptions, type SuggestionState } from '@usememos/editor';\nimport { type CSSProperties, type ForwardedRef, forwardRef, useEffect, useRef, useState } from 'react';\nimport { flushSync } from 'react-dom';\n\nexport type { Editor, SuggestionState } from '@usememos/editor';\n\n/** Constructor options that the React components take as props. */\ntype EditorProps = Pick<\n EditorOptions,\n | 'markdown'\n | 'extensions'\n | 'placeholder'\n | 'editable'\n | 'taskToggle'\n | 'autofocus'\n | 'attributes'\n | 'resolveImageSrc'\n | 'theme'\n | 'toolbar'\n | 'slashMenu'\n | 'labels'\n | 'icons'\n>;\n\nexport interface MarkdownEditorProps extends EditorProps {\n /** Markdown. The editor updates only when this is not a value it emitted itself. */\n value?: string;\n /** Initial Markdown for uncontrolled use. */\n defaultValue?: string;\n /** Called after document changes, at most once per animation frame. */\n onChange?: (markdown: string) => void;\n /** Called on Mod+Enter or Ctrl+Enter with the current Markdown. */\n onSubmit?: (markdown: string) => void;\n onFocus?: () => void;\n onBlur?: () => void;\n className?: string;\n style?: CSSProperties;\n}\n\n/** Options that `setOptions` applies without recreating the editor. `extensions` updates suggestion callbacks only. */\nconst LIVE = ['placeholder', 'editable', 'taskToggle', 'attributes', 'resolveImageSrc', 'theme', 'extensions'] as const;\n\n/** The extension list's identity for recreating the editor: names, so an inline array does not recreate it on every render. */\nconst extensionKey = (extensions: EditorOptions['extensions']) => (extensions ?? []).map((e) => e.name).join('\\n');\n\n/**\n * The editor as a controlled React component. A `ref` gives the core `Editor`.\n *\n * `placeholder`, `editable`, `taskToggle`, `attributes`, `resolveImageSrc` and `theme` update live.\n * A change to the names in `extensions` recreates the editor, keeping its\n * Markdown; with the same names, the callbacks of `suggestion()` extensions\n * (`items`, `command`, `match`, `allow`) update live. Other props are read\n * when the editor is created; change the component's `key` to apply new values.\n *\n * A `value` that arrives while an input method is composing text is applied\n * when the composition ends, so it does not break the composition.\n */\nexport const MarkdownEditor = forwardRef(function MarkdownEditor(props: MarkdownEditorProps, ref: ForwardedRef<Editor | null>) {\n const container = useRef<HTMLDivElement>(null);\n const editorRef = useRef<Editor | null>(null);\n // Values the editor emitted that the parent has not passed back yet, oldest\n // first. While the user keeps typing, React may hand back an older one;\n // those echoes must not reset the document.\n const pending = useRef<string[]>([]);\n // An outside value that arrived during IME composition.\n const deferred = useRef<string | null>(null);\n // Callbacks are read through a ref so changing them does not recreate the editor.\n const latest = useRef(props);\n latest.current = props;\n const extensions = extensionKey(props.extensions);\n\n useEffect(() => {\n const { value, defaultValue, onChange, onSubmit, onFocus, onBlur, className, style, ...options } = latest.current;\n const previous = editorRef.current;\n const editor = new Editor({\n ...options,\n element: container.current!,\n content: previous?.getMarkdown() ?? value ?? defaultValue ?? '',\n onChange: (markdown) => {\n pending.current.push(markdown);\n if (pending.current.length > 100) pending.current.shift();\n // Render the parent's update now, as React does for a native input's\n // onChange. Left to React's scheduler, it can still be waiting when\n // the parent sets a new value (clearing the box on submit) that equals\n // the value it last rendered; the value effect would then not run.\n flushSync(() => latest.current.onChange?.(markdown));\n },\n onSubmit: (markdown) => latest.current.onSubmit?.(markdown),\n onFocus: () => latest.current.onFocus?.(),\n onBlur: () => latest.current.onBlur?.(),\n });\n editorRef.current = editor;\n pending.current = [];\n assignRef(ref, editor);\n const root = container.current!;\n const onCompositionEnd = () => {\n // Runs after the editor's own compositionend handler, which was added first.\n const value = deferred.current;\n deferred.current = null;\n if (value !== null && value !== editor.getMarkdown()) {\n pending.current = [];\n editor.setMarkdown(value);\n }\n };\n root.addEventListener('compositionend', onCompositionEnd);\n return () => {\n root.removeEventListener('compositionend', onCompositionEnd);\n editor.destroy();\n if (editorRef.current === editor) editorRef.current = null;\n assignRef(ref, null);\n };\n // Recreate only when the extension names change; see the note on the component.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [extensions]);\n\n useEffect(() => {\n const editor = editorRef.current;\n if (!editor || props.value === undefined) return;\n const echo = pending.current.indexOf(props.value);\n if (echo >= 0) {\n // The parent caught up to a value the editor emitted.\n pending.current.splice(0, echo + 1);\n return;\n }\n if (props.value === editor.getMarkdown()) return;\n if (editor.isComposing) {\n deferred.current = props.value;\n return;\n }\n pending.current = [];\n editor.setMarkdown(props.value);\n }, [props.value]);\n\n useLiveOptions(editorRef, props);\n\n return <div ref={container} className={props.className} style={props.style} />;\n});\n\n/** Applies the live options when they change. */\nfunction useLiveOptions(editorRef: { current: Editor | null }, props: EditorProps): void {\n const applied = useRef<{ editor: Editor | null; props: Partial<EditorProps> }>({ editor: null, props: {} });\n useEffect(() => {\n const editor = editorRef.current;\n if (!editor) return;\n if (applied.current.editor !== editor) {\n // A new editor was created with these props.\n applied.current = { editor, props: { ...props } };\n return;\n }\n const changed: Partial<Pick<EditorOptions, (typeof LIVE)[number]>> = {};\n let any = false;\n for (const key of LIVE) {\n if (props[key] === applied.current.props[key]) continue;\n (changed as Record<string, unknown>)[key] = key === 'editable' ? (props.editable ?? true) : key === 'taskToggle' ? (props.taskToggle ?? false) : props[key];\n any = true;\n }\n applied.current = { editor, props: { ...props } };\n if (any) editor.setOptions(changed);\n });\n}\n\n/**\n * Creates an editor for the lifetime of the component, or until a value in\n * `deps` changes. Returns null until the editor exists. Pair it with\n * `<EditorContent editor={editor} />`, which shows it.\n *\n * Callbacks in `options` are read when called, so they may change between\n * renders without recreating the editor. So do the callbacks of\n * `suggestion()` extensions, matched by name.\n */\nexport function useEditor(options: Omit<EditorOptions, 'element'>, deps: readonly unknown[] = []): Editor | null {\n const [editor, setEditor] = useState<Editor | null>(null);\n const latest = useRef(options);\n latest.current = options;\n\n useEffect(() => {\n if (editor && options.extensions) editor.setOptions({ extensions: options.extensions });\n }, [editor, options.extensions]);\n\n useEffect(() => {\n const o = latest.current;\n const element = document.createElement('div');\n const instance = new Editor({\n ...o,\n element,\n // A detached element cannot take focus; EditorContent focuses once mounted.\n autofocus: false,\n onChange: (markdown, e) => latest.current.onChange?.(markdown, e),\n onSubmit: (markdown, e) => latest.current.onSubmit?.(markdown, e),\n onFocus: (e) => latest.current.onFocus?.(e),\n onBlur: (e) => latest.current.onBlur?.(e),\n });\n if (o.autofocus) pendingAutofocus.set(instance, o.autofocus === 'end' ? 'end' : 'start');\n setEditor(instance);\n return () => {\n instance.destroy();\n setEditor(null);\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, deps);\n\n return editor;\n}\n\n/**\n * The open autocomplete popup of `editor`, or of its suggestion `name`, and\n * a re-render on each change. With `render: false` on a suggestion, draw your\n * own popup from it: put it at `state.anchorRect()`, give the listbox\n * `id={state.listboxId}` and each option `id={state.optionId(i)}`, highlight\n * `state.active`, and call `editor.suggestion.accept(i)` on click.\n */\nexport function useSuggestion(editor: Editor | null, name?: string): SuggestionState | null {\n const select = (state: SuggestionState | null | undefined) => (state && (name === undefined || state.name === name) ? state : null);\n const [state, setState] = useState<SuggestionState | null>(() => select(editor?.suggestion.state));\n useEffect(() => {\n if (!editor) {\n setState(null);\n return;\n }\n setState(select(editor.suggestion.state));\n return editor.on('suggestionChange', ({ state }) => setState(select(state)));\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [editor, name]);\n return state;\n}\n\nexport interface EditorContentProps {\n editor: Editor | null;\n className?: string;\n style?: CSSProperties;\n}\n\n/** Shows an editor created with `useEditor`. */\nexport function EditorContent({ editor, className, style }: EditorContentProps) {\n const container = useRef<HTMLDivElement>(null);\n useEffect(() => {\n const host = container.current;\n if (!editor || !host) return;\n host.appendChild(editor.root);\n const autofocus = pendingAutofocus.get(editor);\n if (autofocus) {\n pendingAutofocus.delete(editor);\n editor.commands.focus(autofocus);\n }\n return () => {\n if (editor.root.parentNode === host) host.removeChild(editor.root);\n };\n }, [editor]);\n return <div ref={container} className={className} style={style} />;\n}\n\n/** Where to focus an editor from `useEditor` once `EditorContent` mounts it. */\nconst pendingAutofocus = new WeakMap<Editor, 'start' | 'end'>();\n\nfunction assignRef<T>(ref: ForwardedRef<T>, value: T): void {\n if (typeof ref === 'function') ref(value);\n else if (ref) ref.current = value;\n}\n"],
5
+ "mappings": ";AAAA,SAAS,cAAwD;AACjE,SAAgD,YAAY,WAAW,QAAQ,gBAAgB;AAC/F,SAAS,iBAAiB;AAqIjB;AA/FT,IAAM,OAAO,CAAC,eAAe,YAAY,cAAc,cAAc,mBAAmB,SAAS,YAAY;AAG7G,IAAM,eAAe,CAAC,gBAA6C,cAAc,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,IAAI;AAc1G,IAAM,iBAAiB,WAAW,SAASA,gBAAe,OAA4B,KAAkC;AAC7H,QAAM,YAAY,OAAuB,IAAI;AAC7C,QAAM,YAAY,OAAsB,IAAI;AAI5C,QAAM,UAAU,OAAiB,CAAC,CAAC;AAEnC,QAAM,WAAW,OAAsB,IAAI;AAE3C,QAAM,SAAS,OAAO,KAAK;AAC3B,SAAO,UAAU;AACjB,QAAM,aAAa,aAAa,MAAM,UAAU;AAEhD,YAAU,MAAM;AACd,UAAM,EAAE,OAAO,cAAc,UAAU,UAAU,SAAS,QAAQ,WAAW,OAAO,GAAG,QAAQ,IAAI,OAAO;AAC1G,UAAM,WAAW,UAAU;AAC3B,UAAM,SAAS,IAAI,OAAO;AAAA,MACxB,GAAG;AAAA,MACH,SAAS,UAAU;AAAA,MACnB,SAAS,UAAU,YAAY,KAAK,SAAS,gBAAgB;AAAA,MAC7D,UAAU,CAAC,aAAa;AACtB,gBAAQ,QAAQ,KAAK,QAAQ;AAC7B,YAAI,QAAQ,QAAQ,SAAS,IAA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AACX,UAAI,OAAO,KAAK,eAAe,KAAM,MAAK,YAAY,OAAO,IAAI;AAAA,IACnE;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AACX,SAAO,oBAAC,SAAI,KAAK,WAAW,WAAsB,OAAc;AAClE;AAGA,IAAM,mBAAmB,oBAAI,QAAiC;AAE9D,SAAS,UAAa,KAAsB,OAAgB;AAC1D,MAAI,OAAO,QAAQ,WAAY,KAAI,KAAK;AAAA,WAC/B,IAAK,KAAI,UAAU;AAC9B;",
6
+ "names": ["MarkdownEditor", "value", "state"]
7
7
  }
package/dist/style.css CHANGED
@@ -96,6 +96,9 @@
96
96
  .mbe-root.mbe-readonly .mbe-checkbox {
97
97
  cursor: default;
98
98
  }
99
+ .mbe-root.mbe-readonly.mbe-tasks .mbe-checkbox {
100
+ cursor: pointer;
101
+ }
99
102
  .mbe-root a.mbe-link {
100
103
  cursor: text;
101
104
  }
@@ -159,7 +162,7 @@
159
162
  padding: 4px 8px;
160
163
  min-width: 4em;
161
164
  vertical-align: top;
162
- text-align: left;
165
+ text-align: start;
163
166
  outline: none;
164
167
  }
165
168
  .mbe-root > .mbe-block:first-child {
@@ -196,7 +199,13 @@
196
199
  white-space: normal;
197
200
  }
198
201
  .mbe-ui {
199
- z-index: 1000;
202
+ z-index: var(--mbe-popup-z, 1000);
203
+ }
204
+ .mbe-icon {
205
+ display: block;
206
+ flex: none;
207
+ width: 16px;
208
+ height: 16px;
200
209
  }
201
210
  .mbe-ui[hidden] {
202
211
  display: none !important;
@@ -244,7 +253,26 @@
244
253
  .mbe-menu-label {
245
254
  flex: 1;
246
255
  }
247
- .mbe-menu-empty {
256
+ .mbe-suggestion-menu {
257
+ max-width: 360px;
258
+ }
259
+ .mbe-suggestion-menu .mbe-menu-label {
260
+ min-width: 0;
261
+ overflow: hidden;
262
+ text-overflow: ellipsis;
263
+ white-space: nowrap;
264
+ }
265
+ .mbe-menu-detail {
266
+ flex: none;
267
+ white-space: nowrap;
268
+ }
269
+ .mbe-menu-match {
270
+ background: none;
271
+ color: inherit;
272
+ font-weight: 600;
273
+ }
274
+ .mbe-menu-empty,
275
+ .mbe-menu-loading {
248
276
  padding: 6px 8px;
249
277
  }
250
278
  .mbe-toolbar {
@@ -258,8 +286,12 @@
258
286
  color: inherit;
259
287
  background: none;
260
288
  border: none;
261
- padding: 4px 8px;
289
+ padding: 4px 6px;
262
290
  min-width: 28px;
291
+ min-height: 28px;
292
+ display: inline-flex;
293
+ align-items: center;
294
+ justify-content: center;
263
295
  cursor: pointer;
264
296
  }
265
297
  .mbe-tool.mbe-active {
@@ -291,6 +323,9 @@
291
323
  border: none;
292
324
  background: none;
293
325
  color: inherit;
326
+ display: flex;
327
+ align-items: center;
328
+ justify-content: center;
294
329
  cursor: pointer;
295
330
  }
296
331
 
@@ -538,7 +573,15 @@
538
573
  font-weight: 600;
539
574
  color: var(--mbe-muted);
540
575
  }
541
- .mbe-menu-empty {
576
+ .mbe-menu-item.mbe-danger .mbe-menu-icon {
577
+ color: inherit;
578
+ }
579
+ .mbe-menu-empty,
580
+ .mbe-menu-loading {
581
+ color: var(--mbe-muted);
582
+ }
583
+ .mbe-menu-detail {
584
+ font-size: 12px;
542
585
  color: var(--mbe-muted);
543
586
  }
544
587
  .mbe-tool {
@@ -580,8 +623,6 @@
580
623
  .mbe-handle {
581
624
  border-radius: 4px;
582
625
  color: var(--mbe-muted);
583
- font-size: 12px;
584
- letter-spacing: -2px;
585
626
  opacity: 0.7;
586
627
  }
587
628
  .mbe-handle:hover {
package/dist/theme.css CHANGED
@@ -185,7 +185,10 @@
185
185
  .mbe-menu-item.mbe-highlighted { background: var(--mbe-hover); color: inherit; }
186
186
  .mbe-menu-item.mbe-danger { color: var(--mbe-danger); }
187
187
  .mbe-menu-icon { border-radius: 4px; font-size: 11px; font-weight: 600; color: var(--mbe-muted); }
188
- .mbe-menu-empty { color: var(--mbe-muted); }
188
+ .mbe-menu-item.mbe-danger .mbe-menu-icon { color: inherit; }
189
+ .mbe-menu-empty,
190
+ .mbe-menu-loading { color: var(--mbe-muted); }
191
+ .mbe-menu-detail { font-size: 12px; color: var(--mbe-muted); }
189
192
 
190
193
  .mbe-tool { border-radius: 6px; }
191
194
  .mbe-tool:hover { background: var(--mbe-hover); }
@@ -207,8 +210,6 @@
207
210
  .mbe-handle {
208
211
  border-radius: 4px;
209
212
  color: var(--mbe-muted);
210
- font-size: 12px;
211
- letter-spacing: -2px;
212
213
  opacity: 0.7;
213
214
  }
214
215
  .mbe-handle:hover { background: var(--mbe-hover); opacity: 1; }
@@ -80,6 +80,12 @@ export declare function toggleBlockType(state: EditorState, spec: BlockTypeSpec)
80
80
  export declare function insertBlock(state: EditorState, spec: BlockTypeSpec): EditorState;
81
81
  /** Tab / Shift+Tab on list items: changes nesting by one level. */
82
82
  export declare function indentListItems(state: EditorState, delta: 1 | -1): EditorState;
83
+ /**
84
+ * Tab / Shift+Tab in a code block. Tab at a caret inserts two spaces; over a
85
+ * selection it indents every line it touches. Shift+Tab removes up to two
86
+ * leading spaces (or a tab) from those lines.
87
+ */
88
+ export declare function indentCode(state: EditorState, delta: 1 | -1): EditorState;
83
89
  export declare function toggleTask(state: EditorState, blockId: string): EditorState;
84
90
  /** Sets a code block's language, from the input in its header. */
85
91
  export declare function setCodeLanguage(state: EditorState, blockId: string, language: string): EditorState;
@@ -131,6 +137,21 @@ export declare function applyInlineInputRules(state: EditorState): EditorState |
131
137
  * verbatim instead of escaped. Only plain text on the caret's line counts.
132
138
  */
133
139
  export declare function applyTokenInputRules(state: EditorState, syntax: readonly InlineSyntax[]): EditorState | null;
140
+ /**
141
+ * Removes the token of extension syntax from text that no longer reads as
142
+ * that syntax, such as `^1]` left after deleting the `[` of `[^1]`, so it is
143
+ * shown and saved as the text it now is. Text marked as code loses its token
144
+ * too. A run of one token may hold several tokens written next to each other.
145
+ * Only blocks that changed since `before` are checked.
146
+ */
147
+ export declare function dropBrokenTokens(state: EditorState, before: EditorState, syntax: readonly InlineSyntax[]): EditorState;
148
+ /**
149
+ * Lowers list items nested deeper than the block above allows: one level
150
+ * below a list item, or not nested after any other block. Markdown cannot
151
+ * write a nested item without its parent, so this is what saving would give.
152
+ * It happens when a parent item becomes another kind of block or is removed.
153
+ */
154
+ export declare function clampListIndents(state: EditorState, before: EditorState): EditorState;
134
155
  /**
135
156
  * A GFM autolink literal (a `www.`, `http(s)://` or `ftp://` URL, or an
136
157
  * e-mail address) just followed by a typed space becomes a link, the same
@@ -1,7 +1,9 @@
1
1
  import { type ChangeKind } from './history.js';
2
2
  import { type Labels } from './labels.js';
3
+ import { type Icons } from './icons.js';
3
4
  import { type MarkdownOptions } from './markdown/options.js';
4
5
  import type { Extension } from './extension.js';
6
+ import { type SuggestionState } from './suggest/controller.js';
5
7
  import { type Widget, type WidgetRender } from './widgets.js';
6
8
  import { type Block, type EditorState, type HeadingLevel, type Position, type Selection } from './model.js';
7
9
  import { type Theme } from './theme.js';
@@ -18,6 +20,8 @@ export interface EditorOptions {
18
20
  placeholder?: string;
19
21
  /** Default true. */
20
22
  editable?: boolean;
23
+ /** While `editable` is false, lets the user tick and untick task items. Default false. */
24
+ taskToggle?: boolean;
21
25
  /** Focus on creation: `true` or `'start'` puts the caret at the start, `'end'` at the end. */
22
26
  autofocus?: boolean | 'start' | 'end';
23
27
  /** Attributes for the editable root, such as `autocorrect` or `aria-label`. */
@@ -28,8 +32,16 @@ export interface EditorOptions {
28
32
  theme?: Theme;
29
33
  /** Overrides UI strings. */
30
34
  labels?: Partial<Labels>;
35
+ /** Replaces built-in icons, by the `labels` key of the same control. A string is shown as text; an element is cloned. */
36
+ icons?: Icons;
31
37
  /** Shows the formatting toolbar on selection. Default true. */
32
38
  toolbar?: boolean;
39
+ /**
40
+ * The element menus, the toolbar and other popups are appended to.
41
+ * Default: the nearest open `<dialog>` or `aria-modal` ancestor of
42
+ * `element`, else `document.body`.
43
+ */
44
+ popupContainer?: HTMLElement;
33
45
  /** IDs of built-in slash menu items to show, in order, or false for no slash menu. See SLASH_ITEM_IDS. */
34
46
  slashMenu?: false | readonly SlashItemId[];
35
47
  /** Shortcut for `on('change')`, with the new Markdown. */
@@ -40,7 +52,7 @@ export interface EditorOptions {
40
52
  onBlur?: (editor: Editor) => void;
41
53
  }
42
54
  /** Options that `setOptions` can change while the editor runs. */
43
- export type RuntimeOptions = Pick<EditorOptions, 'placeholder' | 'editable' | 'attributes' | 'resolveImageSrc' | 'theme'>;
55
+ export type RuntimeOptions = Pick<EditorOptions, 'placeholder' | 'editable' | 'taskToggle' | 'attributes' | 'resolveImageSrc' | 'theme' | 'extensions'>;
44
56
  export interface EditorEvents {
45
57
  /** After document changes, at most once per animation frame. */
46
58
  change: {
@@ -62,6 +74,27 @@ export interface EditorEvents {
62
74
  submit: {
63
75
  editor: Editor;
64
76
  };
77
+ /** The autocomplete popup opened, closed, or changed its query, items, highlight or loading state. */
78
+ suggestionChange: {
79
+ editor: Editor;
80
+ state: SuggestionState | null;
81
+ };
82
+ }
83
+ export interface SuggestionApi {
84
+ /** The open suggestion: its query, range, items and highlighted item. Null when none is open. */
85
+ readonly state: SuggestionState | null;
86
+ /**
87
+ * Opens the popup for the token at the caret, as Ctrl+Space does, even
88
+ * where typing would not open it. With `name`, only that suggestion.
89
+ * Returns false when there is no token at the caret.
90
+ */
91
+ open(name?: string): boolean;
92
+ /** Closes the popup and keeps it closed for this token, as Escape does. */
93
+ close(): void;
94
+ /** Moves the highlight by `delta` items, wrapping around, as the arrow keys do. */
95
+ move(delta: number): void;
96
+ /** Picks the item at `index`, or the highlighted one, as Enter does. Returns false when there is none. */
97
+ accept(index?: number): boolean;
65
98
  }
66
99
  /** Names `isActive` and `getAttributes` understand. */
67
100
  export type ActiveName = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'paragraph' | 'heading' | 'bulletList' | 'orderedList' | 'taskList' | 'blockquote' | 'codeBlock' | 'table' | 'divider' | 'raw';
@@ -131,13 +164,17 @@ export interface EditorCommands {
131
164
  selectBlocks(): boolean;
132
165
  focus(position?: 'start' | 'end' | Position): boolean;
133
166
  blur(): boolean;
167
+ /** Scrolls the caret (the selection's focus end) into view, in scrolling containers and the window. */
168
+ scrollIntoView(): boolean;
134
169
  }
135
170
  export declare const SLASH_ITEM_IDS: readonly ["text", "heading1", "heading2", "heading3", "bulletList", "numberedList", "todoList", "quote", "code", "table", "divider"];
136
171
  export type SlashItemId = (typeof SLASH_ITEM_IDS)[number];
137
172
  export declare class Editor {
173
+ /** The editable element the editor renders into: `options.element`, or for `useEditor`, the element `<EditorContent>` mounts. */
138
174
  readonly root: HTMLElement;
139
- readonly labels: Labels;
140
175
  readonly commands: EditorCommands;
176
+ /** The autocomplete popup of `suggestion()` extensions and the slash menu. */
177
+ readonly suggestion: SuggestionApi;
141
178
  private current;
142
179
  /** The Markdown the document was loaded from; unchanged blocks are saved as this text. */
143
180
  private source;
@@ -151,11 +188,15 @@ export declare class Editor {
151
188
  * block, the marks for composed text, and whether it has an undo step yet.
152
189
  */
153
190
  private composition;
191
+ /** When the last composition ended (Safari only): the next key may be the Enter that committed it. */
192
+ private compositionEndedAt;
154
193
  /** Blocks the browser edited during a composition that ended, rendered again on the next frame. */
155
194
  private readonly unsettled;
156
195
  private settleFrame;
157
196
  private readOnly;
158
197
  private wasReadOnly;
198
+ /** Task checkboxes stay usable while read-only. */
199
+ private taskToggle;
159
200
  private readonly options;
160
201
  /** The extensions in priority order, ending with the default keymap unless one was given. */
161
202
  private readonly extensions;
@@ -174,10 +215,18 @@ export declare class Editor {
174
215
  private focused;
175
216
  /** A mouse button is down in the editor. Rendered views keep their state until it is released, so a drag is not disturbed. */
176
217
  private pointerDown;
177
- /** Suggestion menus from extensions, then the slash menu. */
218
+ /** The selection the formatting toolbar was closed for with Escape. It stays closed until the selection changes. */
219
+ private toolbarClosedFor;
220
+ /** Handling a key press, typed text, a paste or a drop: changes then scroll the caret into view. */
221
+ private typing;
222
+ /**
223
+ * A caret key the browser is handling: the block the caret was in, the
224
+ * caret's x, and 1 or -1 for a plain ArrowDown or ArrowUp. The next
225
+ * selection change checks where the caret went (see `onSelectionChange`).
226
+ */
227
+ private caretKey;
228
+ /** Suggestion menus from extensions, then the slash menu (see suggest/controller.ts). */
178
229
  private readonly suggestions;
179
- /** The open suggestion: which one, where its trigger is, and the last query shown. */
180
- private suggest;
181
230
  private readonly blockMenu;
182
231
  private readonly toolbar;
183
232
  private readonly linkPopover;
@@ -209,6 +258,8 @@ export declare class Editor {
209
258
  /** True while an input method (IME) is composing text. Replacing the document now would break the composition. */
210
259
  get isComposing(): boolean;
211
260
  get isEditable(): boolean;
261
+ /** True after `destroy()`. The editor then ignores every call that would change it; `getMarkdown()` still returns its last Markdown. */
262
+ get isDestroyed(): boolean;
212
263
  /** True if `name` applies at the selection: a mark on the selected text, or the type of the block with the caret. */
213
264
  isActive(name: ActiveName, attrs?: Readonly<Record<string, unknown>>): boolean;
214
265
  /** Attributes of `name` at the selection: `href` for a link, `level` for a heading, `language` for a code block, `checked` for a task, `syntax` for a raw block. */
@@ -241,7 +292,12 @@ export declare class Editor {
241
292
  /** Focuses the editor at the current selection. */
242
293
  focus(): void;
243
294
  blur(): void;
244
- /** Changes `placeholder`, `editable`, `attributes`, `resolveImageSrc` or `theme`. */
295
+ /**
296
+ * Changes `placeholder`, `editable`, `taskToggle`, `attributes`,
297
+ * `resolveImageSrc` or `theme`. `extensions` only updates the callbacks of
298
+ * `suggestion()` extensions (`items`, `command`, `match`, `allow`), matched
299
+ * by name; other changes to extensions need a new editor.
300
+ */
245
301
  setOptions(options: RuntimeOptions): void;
246
302
  /** Subscribes to an event. Returns a function that unsubscribes. */
247
303
  on<K extends keyof EditorEvents>(type: K, handler: (payload: EditorEvents[K]) => void): () => void;
@@ -255,10 +311,8 @@ export declare class Editor {
255
311
  private toggleMark;
256
312
  private undo;
257
313
  private redo;
258
- /** @internal Viewport rectangle of the selection, for positioning UI. */
259
- selectionRect(): DOMRect | null;
260
- /** @internal The rendered element of a block. */
261
- blockElement(id: string): HTMLElement | undefined;
314
+ /** Scrolls the editor and the elements around it so the caret is visible. */
315
+ private scrollCaretIntoView;
262
316
  destroy(): void;
263
317
  private apply;
264
318
  private commit;
@@ -277,12 +331,26 @@ export declare class Editor {
277
331
  * or has just changed.
278
332
  */
279
333
  private applyReadOnly;
334
+ /** True while read-only with task toggling on: only task checkboxes respond. */
335
+ private get ticking();
336
+ /** Toggles a task item from its checkbox, while editing or while read-only with `taskToggle`. */
337
+ private toggleTaskAt;
280
338
  /** Hints for empty blocks, and the UI that follows the selection. */
281
339
  private afterRender;
282
340
  private syncDomSelection;
283
341
  private toDom;
284
342
  private readDomSelection;
285
343
  private onSelectionChange;
344
+ /**
345
+ * After the browser moved the caret from block `from` with ArrowDown
346
+ * (`dir` 1) or ArrowUp to `to`: the position in the first block it passed
347
+ * over that holds a caret, at `x`. Firefox passes over list items (they are
348
+ * flex boxes), WebKit over code blocks and tables. Null when no block was
349
+ * passed over.
350
+ */
351
+ private skippedBlock;
352
+ /** Where the caret goes moving into `block` from above (`forward`) or below: its first or last line, at `x`. */
353
+ private blockEntry;
286
354
  /**
287
355
  * Renders the document, then puts the browser selection back. Only blocks
288
356
  * whose object or revealed views changed are rebuilt, unless `force`.
@@ -295,10 +363,26 @@ export declare class Editor {
295
363
  private onMouseUp;
296
364
  /**
297
365
  * When an arrow key at the edge of a block would move past a rendered raw
298
- * block next to it (the browser skips non-editable blocks), the position in
299
- * that block's source to move to instead.
366
+ * block or a table next to it, the position inside that block to move to
367
+ * instead: the browser skips non-editable blocks, and sometimes a table
368
+ * (its cells are separate editing hosts). A table is entered in its first
369
+ * column.
370
+ */
371
+ private blockPast;
372
+ /**
373
+ * ArrowUp or ArrowDown on the first or last line of a table cell: the
374
+ * position in the cell above or below (same column), or past the table in
375
+ * the next block that holds a caret. Null when the caret is not on that
376
+ * line, or when the table ends the document that way (`exitBlock` adds a
377
+ * paragraph there).
378
+ */
379
+ private tableCellPast;
380
+ /**
381
+ * Whether the caret is on the last (`forward`) or first line of the text in
382
+ * `content`. Lines are measured from the text, not the element's box, which
383
+ * has padding (a table cell) and may be taller than its text.
300
384
  */
301
- private viewBlockPast;
385
+ private onEdgeLine;
302
386
  /** A click on a rendered view puts the caret in its source: at the end of a token, or of a block's formula. */
303
387
  private revealView;
304
388
  /** The browser selection is the source of truth when an event starts. */
@@ -321,6 +405,14 @@ export declare class Editor {
321
405
  private onCompositionEnd;
322
406
  private settleComposition;
323
407
  private onInput;
408
+ /** The length of the composed block's or cell's text. */
409
+ private composedLength;
410
+ /**
411
+ * Ends the composition on purpose: reads the composed text into the model
412
+ * and renders the block from it, which ends the browser's composition.
413
+ * Picking a suggestion item while composing does this first.
414
+ */
415
+ private endComposition;
324
416
  /**
325
417
  * Reads the composed text into the model while composing. The browser's
326
418
  * DOM for the block already shows it, so the block is not re-rendered.
@@ -338,6 +430,8 @@ export declare class Editor {
338
430
  */
339
431
  private readDom;
340
432
  private onKeyDown;
433
+ /** Selects all the text of the block (or table cell) holding `target`. False if it holds none. */
434
+ private selectBlockText;
341
435
  private moveMenu;
342
436
  private onMouseDown;
343
437
  /**
@@ -359,15 +453,8 @@ export declare class Editor {
359
453
  private onDrop;
360
454
  /** The document position at a viewport point, or null outside the editor's text. */
361
455
  positionAtPoint(x: number, y: number): Position | null;
362
- /** Opens the suggestion menu whose trigger was just typed, if any. */
363
- private maybeOpenSuggestion;
364
- private closeSuggestion;
365
- /** The text typed after the trigger, or null when the suggestion should close. */
366
- private suggestionQuery;
367
- /** Asks for items when the query changed, and shows them when they arrive. */
368
- private refreshSuggestion;
369
- private placeSuggestion;
370
- private pickSuggestion;
456
+ /** After typing: opens the suggestion whose token is at the caret, if any. */
457
+ private typedForSuggestion;
371
458
  /** The slash menu, as a suggestion. */
372
459
  private slashSuggestion;
373
460
  /** The blocks of the block selection, in document order. Empty when the selection is text. */
@@ -392,17 +479,11 @@ export declare class Editor {
392
479
  */
393
480
  private onBlockKey;
394
481
  private blockMenuSections;
395
- /**
396
- * Only large blocks get the handle and block menu: tables and code blocks.
397
- * Other blocks change type through the slash menu or Markdown shortcuts.
398
- */
399
- hasBlockMenu(blockId: string): boolean;
400
- openBlockMenu(blockId: string, anchor?: DOMRect): void;
401
482
  private closeBlockMenu;
402
- openLinkPopover(): void;
403
483
  private updateUi;
404
- private openMenu;
405
484
  private syncActiveDescendant;
406
485
  private hideUi;
486
+ /** Wraps an input event handler so the changes it makes keep the caret in view. */
487
+ private typed;
407
488
  private listen;
408
489
  }