@dou-ui/editor 0.1.0-alpha.1 → 0.1.0-alpha.3
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/code/code-editor-block.js.map +1 -1
- package/dist/code/code-editor-block.vue_vue_type_script_setup_true_lang.js +1 -0
- package/dist/code/code-editor-block.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/block-handle.js.map +1 -1
- package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js +1 -1
- package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/bubble-toolbar.js.map +1 -1
- package/dist/components/bubble-toolbar.vue_vue_type_script_setup_true_lang.js +2 -1
- package/dist/components/bubble-toolbar.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/comment-card.js.map +1 -1
- package/dist/components/comment-card.vue_vue_type_script_setup_true_lang.js +2 -1
- package/dist/components/comment-card.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/core/editor.js +2 -1
- package/dist/core/editor.js.map +1 -1
- package/dist/core/plugins.js +108 -5
- package/dist/core/plugins.js.map +1 -1
- package/dist/dom.js +6 -1
- package/dist/dom.js.map +1 -1
- package/dist/extensions/align/align-menu.js.map +1 -1
- package/dist/extensions/align/align-menu.vue_vue_type_script_setup_true_lang.js +4 -6
- package/dist/extensions/align/align-menu.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/extensions/colors/color-menu.js.map +1 -1
- package/dist/extensions/colors/color-menu.vue_vue_type_script_setup_true_lang.js +6 -8
- package/dist/extensions/colors/color-menu.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/style.js +0 -1
- package/dist/types/core/plugins.d.ts +9 -0
- package/dist/types/dom.d.ts +2 -0
- package/package.json +5 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"comment-card.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createBlock","_Transition","_withCtx","_createElementBlock","_unref","_normalizeStyle","_createCommentVNode"],"sources":["../../src/components/comment-card.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport {\n DouCommentThread,\n useFloating,\n useLayer,\n useZIndex,\n type CommentThread,\n type VirtualElement,\n} from 'dou-ui'\nimport { computed, inject, nextTick, shallowRef, watch } from 'vue'\nimport { commentRange, type CommentRange } from '../core/comments'\nimport { editorKey } from '../editor'\n\n/*\n * The thread a piece of text is about, beside that text: opened by a\n * click on highlighted text or `v-model:activeComment`, or empty while\n * a new thread's first comment is being written.\n */\n\nconst { view, state, comments, messages } = inject(editorKey)!\nconst panel = shallowRef<HTMLElement | null>(null)\n\nconst thread = computed<CommentThread | null>(() => {\n const id = comments.active.value\n return (id && comments.threads.value.find((item) => item.id === id)) || null\n})\n\nconst range = computed<CommentRange | null>(() => {\n if (comments.pending.value) return comments.pending.value\n const current = state.value\n return thread.value && current ? commentRange(current.doc, thread.value.id) : null\n})\n\nconst open = computed(() => !!range.value)\n\nconst anchor: VirtualElement = {\n getBoundingClientRect() {\n const current = view.value\n const at = range.value\n if (!current || !at) return new DOMRect()\n const start = current.coordsAtPos(at.from, 1)\n const end = current.coordsAtPos(at.to, -1)\n // text over several lines: from its start to the
|
|
1
|
+
{"version":3,"file":"comment-card.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createBlock","_Transition","_withCtx","_createElementBlock","_unref","_normalizeStyle","_createCommentVNode"],"sources":["../../src/components/comment-card.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport {\n DouCommentThread,\n useFloating,\n useLayer,\n useZIndex,\n type CommentThread,\n type VirtualElement,\n} from 'dou-ui'\nimport { computed, inject, nextTick, shallowRef, watch } from 'vue'\nimport { commentRange, type CommentRange } from '../core/comments'\nimport { editorKey } from '../editor'\nimport { columnRight } from '../dom'\n\n/*\n * The thread a piece of text is about, beside that text: opened by a\n * click on highlighted text or `v-model:activeComment`, or empty while\n * a new thread's first comment is being written.\n */\n\nconst { view, state, comments, messages } = inject(editorKey)!\nconst panel = shallowRef<HTMLElement | null>(null)\n\nconst thread = computed<CommentThread | null>(() => {\n const id = comments.active.value\n return (id && comments.threads.value.find((item) => item.id === id)) || null\n})\n\nconst range = computed<CommentRange | null>(() => {\n if (comments.pending.value) return comments.pending.value\n const current = state.value\n return thread.value && current ? commentRange(current.doc, thread.value.id) : null\n})\n\nconst open = computed(() => !!range.value)\n\nconst anchor: VirtualElement = {\n getBoundingClientRect() {\n const current = view.value\n const at = range.value\n if (!current || !at) return new DOMRect()\n const start = current.coordsAtPos(at.from, 1)\n const end = current.coordsAtPos(at.to, -1)\n // text over several lines: from its start to the text column's right edge\n const right =\n Math.abs(start.top - end.top) < 4\n ? end.right\n : Math.max(start.left + 1, columnRight(current.dom))\n return new DOMRect(\n start.left,\n start.top,\n Math.max(1, right - start.left),\n end.bottom - start.top,\n )\n },\n}\n\nconst { style, placement, update } = useFloating(() => (open.value ? anchor : null), panel, {\n open,\n placement: 'bottom-start',\n offset: 6,\n})\nconst { next } = useZIndex()\nconst zIndex = shallowRef(0)\nwatch(open, (value) => value && (zIndex.value = next()), { immediate: true })\nwatch(state, () => open.value && void nextTick(update))\n\nconst threadView = shallowRef<InstanceType<typeof DouCommentThread> | null>(null)\n\n/** Focus was in the card, or went nowhere when what held it went away. */\nconst focusLost = () => {\n const el = document.activeElement\n return !el || el === document.body || !!panel.value?.contains(el)\n}\n\nfunction close() {\n const refocus = focusLost()\n comments.close()\n if (refocus) view.value?.focus()\n}\n\n// the card is a layer: Escape goes to a menu opened from it first, and a\n// press on such a menu isn't outside. A press on the editor's text is the\n// editor's: it opens the thread there, or closes the card.\nuseLayer({\n open,\n elements: () => [panel.value, view.value?.dom],\n onEscape: () => close(),\n onOutside: () => comments.close(),\n})\n\n// asked to (by the keyboard shortcut, or a thread just started): the reply box\nwatch(\n () => comments.focusRequest.value,\n async () => {\n await nextTick()\n await nextTick()\n threadView.value?.focus()\n },\n)\n\n// a thread resolved or removed from the card: back to the text, once\n// the card is gone (focus can sit in it while it fades out)\nfunction onClosed() {\n const el = document.activeElement\n if (!el || el === document.body) view.value?.focus()\n}\n</script>\n\n<template>\n <Transition name=\"dou-floating\" @after-leave=\"onClosed\">\n <div\n v-if=\"open\"\n ref=\"panel\"\n class=\"dou-floating dou-editor-comment\"\n role=\"dialog\"\n :aria-label=\"messages.commentThread\"\n :style=\"[style, { zIndex }]\"\n :data-placement=\"placement\"\n >\n <DouCommentThread\n ref=\"threadView\"\n :key=\"thread?.id ?? 'new'\"\n :thread=\"thread\"\n :author=\"comments.author.value\"\n :readonly=\"!comments.writable.value\"\n :autofocus=\"!thread\"\n @action=\"comments.apply\"\n @cancel=\"close\"\n />\n </div>\n </Transition>\n</template>\n"],"mappings":";;;;;;;;;;EAoBA,MAAM,EAAE,MAAM,OAAO,UAAU,aAAa,OAAO,SAAS;EAC5D,MAAM,QAAQ,WAA+B,IAAI;EAEjD,MAAM,SAAS,eAAqC;GAClD,MAAM,KAAK,SAAS,OAAO;GAC3B,OAAQ,MAAM,SAAS,QAAQ,MAAM,MAAM,SAAS,KAAK,OAAO,EAAE,KAAM;EAC1E,CAAC;EAED,MAAM,QAAQ,eAAoC;GAChD,IAAI,SAAS,QAAQ,OAAO,OAAO,SAAS,QAAQ;GACpD,MAAM,UAAU,MAAM;GACtB,OAAO,OAAO,SAAS,UAAU,aAAa,QAAQ,KAAK,OAAO,MAAM,EAAE,IAAI;EAChF,CAAC;EAED,MAAM,OAAO,eAAe,CAAC,CAAC,MAAM,KAAK;EAEzC,MAAM,SAAyB,EAC7B,wBAAwB;GACtB,MAAM,UAAU,KAAK;GACrB,MAAM,KAAK,MAAM;GACjB,IAAI,CAAC,WAAW,CAAC,IAAI,OAAO,IAAI,QAAQ;GACxC,MAAM,QAAQ,QAAQ,YAAY,GAAG,MAAM,CAAC;GAC5C,MAAM,MAAM,QAAQ,YAAY,GAAG,IAAI,EAAE;GAEzC,MAAM,QACJ,KAAK,IAAI,MAAM,MAAM,IAAI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A,SADRH,UAAA,GAAAI,mBAmBM,OAAA;;KAjBA,SAAA;KAAJ,KAAI;KACJ,OAAM;KACN,MAAK;KACJ,cAAYC,MAAA,QAAA,CAAQ,CAAC;KACrB,OAAKC,eAAA,CAAGD,MAAA,KAAA,GAAK,EAAA,QAAI,OAAA,MAAM,CAAA,CAAA;KACvB,kBAAgBA,MAAA,SAAA;I,KAEjBL,UAAA,GAAAC,YASEI,MAAA,gBAAA,GAAA;KARI,SAAA;KAAJ,KAAI;KACH,KAAK,OAAA,OAAQ,MAAE;KACf,QAAQ,OAAA;KACR,QAAQA,MAAA,QAAA,CAAQ,CAAC,OAAO;KACxB,UAAQ,CAAGA,MAAA,QAAA,CAAQ,CAAC,SAAS;KAC7B,WAAS,CAAG,OAAA;KACZ,UAAQA,MAAA,QAAA,CAAQ,CAAC;KACjB,UAAQ"}
|
package/dist/core/editor.js
CHANGED
|
@@ -2,7 +2,7 @@ import { blocksToDoc, docToBlocks } from "./model.js";
|
|
|
2
2
|
import { blockSelectionPlugin } from "./block-selection.js";
|
|
3
3
|
import { editorInputRules } from "./input-rules.js";
|
|
4
4
|
import { editorKeymap } from "./keymap.js";
|
|
5
|
-
import { idPlugin, placeholderPlugin } from "./plugins.js";
|
|
5
|
+
import { idPlugin, listPlugin, placeholderPlugin } from "./plugins.js";
|
|
6
6
|
import { preparePastePlugin } from "./prepare.js";
|
|
7
7
|
import { keymap } from "prosemirror-keymap";
|
|
8
8
|
import { EditorState, Plugin, PluginKey } from "prosemirror-state";
|
|
@@ -52,6 +52,7 @@ function createPlugins(options) {
|
|
|
52
52
|
...builtInHistory ? [history()] : [],
|
|
53
53
|
preparePastePlugin(kit),
|
|
54
54
|
idPlugin(),
|
|
55
|
+
listPlugin(),
|
|
55
56
|
placeholderPlugin(options.placeholder)
|
|
56
57
|
];
|
|
57
58
|
}
|
package/dist/core/editor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"editor.js","names":[],"sources":["../../src/core/editor.ts"],"sourcesContent":["import { baseKeymap } from 'prosemirror-commands'\nimport { history, redo, undo } from 'prosemirror-history'\nimport { keymap } from 'prosemirror-keymap'\nimport type { Node as PMNode } from 'prosemirror-model'\nimport { EditorState, Plugin, PluginKey, type Command, type Transaction } from 'prosemirror-state'\nimport { EditorView, type NodeViewConstructor } from 'prosemirror-view'\nimport { blockSelectionPlugin } from './block-selection'\nimport type { ResolvedEditorExtensions } from './extension'\nimport { editorInputRules } from './input-rules'\nimport { editorKeymap, type KeymapOptions } from './keymap'\nimport { blocksToDoc, docToBlocks, type Block, type PartialBlock } from './model'\nimport { idPlugin, placeholderPlugin, type PlaceholderOptions } from './plugins'\nimport { preparePastePlugin } from './prepare'\n\nexport interface CreateEditorOptions {\n /** Where the editor goes. */\n element: HTMLElement\n /** The resolved extensions: schema, plugins, node views. */\n kit: ResolvedEditorExtensions\n /** More node views, e.g. ones that render Vue components. */\n nodeViews?: Record<string, NodeViewConstructor>\n blocks?: PartialBlock[]\n editable?: boolean\n placeholder: PlaceholderOptions\n /** Called after each change to the document, with the transaction and the state before it. */\n onChange?: (view: EditorView, tr: Transaction, before: EditorState) => void\n /** Called after every state change, including the selection. */\n onUpdate?: (view: EditorView, before: EditorState) => void\n /**\n * Extra plugins, placed before the built-in ones so their keys win.\n * The collaboration extension adds its sync and cursor plugins here.\n */\n plugins?: Plugin[]\n /**\n * Key plugins placed after the extensions' keys, so an extension can\n * take one of their keys, and before the built-in ones.\n */\n keymaps?: Plugin[]\n /** Leave out the built-in undo history (collaboration brings its own). */\n history?: boolean\n undo?: KeymapOptions['undo']\n redo?: KeymapOptions['redo']\n /** Attributes for the editable element, e.g. an accessible name. */\n attributes?: () => Record<string, string>\n}\n\n/** The transaction meta that names who made a change, for the `transaction` event. */\nexport const originKey = 'douOrigin'\n\nconst historyKey = new PluginKey<{ undo: Command; redo: Command }>('douHistory')\n\n/**\n * The undo and redo the editor runs: the built-in history's, or those of\n * the extension or props that replace it. For node views that edit their\n * text themselves, so their undo is the document's.\n */\nexport function historyCommands(state: EditorState): {\n undo: Command\n redo: Command\n} {\n return historyKey.getState(state) ?? { undo, redo }\n}\n\nexport function createPlugins(options: Omit<CreateEditorOptions, 'element'>): Plugin[] {\n const { kit } = options\n const provider = kit.history\n const builtInHistory = options.history !== false && !provider\n const commands = {\n undo: options.undo ?? provider?.undo ?? undo,\n redo: options.redo ?? provider?.redo ?? redo,\n }\n return [\n new Plugin({\n key: historyKey,\n state: { init: () => commands, apply: (_tr, value) => value },\n }),\n ...(options.plugins ?? []),\n ...kit.plugins,\n editorInputRules(kit.inputRules),\n blockSelectionPlugin(),\n ...kit.keymaps.map((bindings) => keymap(bindings)),\n ...(options.keymaps ?? []),\n editorKeymap(commands),\n keymap(baseKeymap),\n ...(provider?.plugins ?? []),\n ...(builtInHistory ? [history()] : []),\n preparePastePlugin(kit),\n idPlugin(),\n placeholderPlugin(options.placeholder),\n ]\n}\n\nexport function createState(\n kit: ResolvedEditorExtensions,\n blocks: PartialBlock[] | undefined,\n plugins: Plugin[],\n): EditorState {\n return EditorState.create({\n schema: kit.schema,\n doc: blocksToDoc(kit.schema, blocks ?? []),\n plugins,\n })\n}\n\nconst viewNodeViews = new WeakMap<EditorView, Record<string, NodeViewConstructor>>()\n\n/** Fresh functions, so the view redraws every node view (e.g. after `editable` changes). */\nconst fresh = (nodeViews: Record<string, NodeViewConstructor>) =>\n Object.fromEntries(\n Object.entries(nodeViews).map(([name, create]) => [\n name,\n ((...args) => create(...args)) as NodeViewConstructor,\n ]),\n )\n\nexport function createEditorView(options: CreateEditorOptions): EditorView {\n const editable = options.editable ?? true\n const plugins = createPlugins(options)\n const nodeViews = { ...options.kit.nodeViews, ...options.nodeViews }\n const view: EditorView = new EditorView(options.element, {\n state: createState(options.kit, options.blocks, plugins),\n editable: () => editable,\n nodeViews: fresh(nodeViews),\n attributes: () => ({\n class: 'dou-editor__surface',\n role: 'textbox',\n 'aria-multiline': 'true',\n ...options.attributes?.(),\n }),\n // `this`, not `view`: plugins (e.g. collaboration) dispatch while the view is being created\n dispatchTransaction(this: EditorView, tr) {\n const before = this.state\n const next = before.apply(tr)\n this.updateState(next)\n if (next.doc !== before.doc) options.onChange?.(this, tr, before)\n options.onUpdate?.(this, before)\n },\n })\n viewNodeViews.set(view, nodeViews)\n return view\n}\n\nexport function setEditable(view: EditorView, value: boolean) {\n // node views draw controls (checkboxes…) that are disabled while read-only\n view.setProps({ editable: () => value, nodeViews: fresh(viewNodeViews.get(view) ?? {}) })\n}\n\n/** Replace the whole document, e.g. when `v-model` changes from outside. */\nexport function replaceBlocks(view: EditorView, blocks: PartialBlock[]) {\n const state = view.state\n const doc = blocksToDoc(state.schema, blocks)\n const tr = state.tr.replaceWith(0, state.doc.content.size, doc.content)\n tr.setMeta('addToHistory', false)\n tr.setMeta(originKey, 'external')\n view.dispatch(tr)\n}\n\n/**\n * Replace the document with `blocks` as one change undo takes back, e.g.\n * an older version. Only what differs is replaced, so the blocks that stay\n * keep their place, and with collaboration only they are written.\n */\nexport function restoreBlocks(view: EditorView, blocks: PartialBlock[]) {\n const state = view.state\n const next = blocksToDoc(state.schema, blocks)\n const start = state.doc.content.findDiffStart(next.content)\n if (start === null) return false\n const end = state.doc.content.findDiffEnd(next.content)!\n // the two ends can cross where the same text repeats: widen them to agree\n const overlap = start - Math.min(end.a, end.b)\n if (overlap > 0) {\n end.a += overlap\n end.b += overlap\n }\n view.dispatch(\n state.tr\n .replace(start, end.a, next.slice(start, end.b))\n .setMeta(originKey, 'restore')\n .scrollIntoView(),\n )\n return true\n}\n\nexport function getBlocks(view: EditorView): Block[] {\n return docToBlocks(view.state.doc)\n}\n\nexport function runCommand(view: EditorView, command: Command): boolean {\n return command(view.state, view.dispatch, view)\n}\n\nexport type { PMNode }\n"],"mappings":";;;;;;;;;;;;;AA+CA,IAAa,YAAY;AAEzB,IAAM,aAAa,IAAI,UAA4C,YAAY;;;;;;AAO/E,SAAgB,gBAAgB,OAG9B;CACA,OAAO,WAAW,SAAS,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,GAAG;EACf,IAAI,KAAK;EACT,IAAI,KAAK;CACX;CACA,KAAK,SACH,MAAM,GACH,QAAQ,OAAO,IAAI,GAAG,KAAK,MAAM,OAAO,IAAI,CAAC,CAAC,CAAC,CAC/C,QAAQ,WAAW,SAAS,CAAC,CAC7B,eAAe,CACpB;CACA,OAAO;AACT;AAEA,SAAgB,UAAU,MAA2B;CACnD,OAAO,YAAY,KAAK,MAAM,GAAG;AACnC;AAEA,SAAgB,WAAW,MAAkB,SAA2B;CACtE,OAAO,QAAQ,KAAK,OAAO,KAAK,UAAU,IAAI;AAChD"}
|
|
1
|
+
{"version":3,"file":"editor.js","names":[],"sources":["../../src/core/editor.ts"],"sourcesContent":["import { baseKeymap } from 'prosemirror-commands'\nimport { history, redo, undo } from 'prosemirror-history'\nimport { keymap } from 'prosemirror-keymap'\nimport type { Node as PMNode } from 'prosemirror-model'\nimport { EditorState, Plugin, PluginKey, type Command, type Transaction } from 'prosemirror-state'\nimport { EditorView, type NodeViewConstructor } from 'prosemirror-view'\nimport { blockSelectionPlugin } from './block-selection'\nimport type { ResolvedEditorExtensions } from './extension'\nimport { editorInputRules } from './input-rules'\nimport { editorKeymap, type KeymapOptions } from './keymap'\nimport { blocksToDoc, docToBlocks, type Block, type PartialBlock } from './model'\nimport { idPlugin, listPlugin, placeholderPlugin, type PlaceholderOptions } from './plugins'\nimport { preparePastePlugin } from './prepare'\n\nexport interface CreateEditorOptions {\n /** Where the editor goes. */\n element: HTMLElement\n /** The resolved extensions: schema, plugins, node views. */\n kit: ResolvedEditorExtensions\n /** More node views, e.g. ones that render Vue components. */\n nodeViews?: Record<string, NodeViewConstructor>\n blocks?: PartialBlock[]\n editable?: boolean\n placeholder: PlaceholderOptions\n /** Called after each change to the document, with the transaction and the state before it. */\n onChange?: (view: EditorView, tr: Transaction, before: EditorState) => void\n /** Called after every state change, including the selection. */\n onUpdate?: (view: EditorView, before: EditorState) => void\n /**\n * Extra plugins, placed before the built-in ones so their keys win.\n * The collaboration extension adds its sync and cursor plugins here.\n */\n plugins?: Plugin[]\n /**\n * Key plugins placed after the extensions' keys, so an extension can\n * take one of their keys, and before the built-in ones.\n */\n keymaps?: Plugin[]\n /** Leave out the built-in undo history (collaboration brings its own). */\n history?: boolean\n undo?: KeymapOptions['undo']\n redo?: KeymapOptions['redo']\n /** Attributes for the editable element, e.g. an accessible name. */\n attributes?: () => Record<string, string>\n}\n\n/** The transaction meta that names who made a change, for the `transaction` event. */\nexport const originKey = 'douOrigin'\n\nconst historyKey = new PluginKey<{ undo: Command; redo: Command }>('douHistory')\n\n/**\n * The undo and redo the editor runs: the built-in history's, or those of\n * the extension or props that replace it. For node views that edit their\n * text themselves, so their undo is the document's.\n */\nexport function historyCommands(state: EditorState): {\n undo: Command\n redo: Command\n} {\n return historyKey.getState(state) ?? { undo, redo }\n}\n\nexport function createPlugins(options: Omit<CreateEditorOptions, 'element'>): Plugin[] {\n const { kit } = options\n const provider = kit.history\n const builtInHistory = options.history !== false && !provider\n const commands = {\n undo: options.undo ?? provider?.undo ?? undo,\n redo: options.redo ?? provider?.redo ?? redo,\n }\n return [\n new Plugin({\n key: historyKey,\n state: { init: () => commands, apply: (_tr, value) => value },\n }),\n ...(options.plugins ?? []),\n ...kit.plugins,\n editorInputRules(kit.inputRules),\n blockSelectionPlugin(),\n ...kit.keymaps.map((bindings) => keymap(bindings)),\n ...(options.keymaps ?? []),\n editorKeymap(commands),\n keymap(baseKeymap),\n ...(provider?.plugins ?? []),\n ...(builtInHistory ? [history()] : []),\n preparePastePlugin(kit),\n idPlugin(),\n listPlugin(),\n placeholderPlugin(options.placeholder),\n ]\n}\n\nexport function createState(\n kit: ResolvedEditorExtensions,\n blocks: PartialBlock[] | undefined,\n plugins: Plugin[],\n): EditorState {\n return EditorState.create({\n schema: kit.schema,\n doc: blocksToDoc(kit.schema, blocks ?? []),\n plugins,\n })\n}\n\nconst viewNodeViews = new WeakMap<EditorView, Record<string, NodeViewConstructor>>()\n\n/** Fresh functions, so the view redraws every node view (e.g. after `editable` changes). */\nconst fresh = (nodeViews: Record<string, NodeViewConstructor>) =>\n Object.fromEntries(\n Object.entries(nodeViews).map(([name, create]) => [\n name,\n ((...args) => create(...args)) as NodeViewConstructor,\n ]),\n )\n\nexport function createEditorView(options: CreateEditorOptions): EditorView {\n const editable = options.editable ?? true\n const plugins = createPlugins(options)\n const nodeViews = { ...options.kit.nodeViews, ...options.nodeViews }\n const view: EditorView = new EditorView(options.element, {\n state: createState(options.kit, options.blocks, plugins),\n editable: () => editable,\n nodeViews: fresh(nodeViews),\n attributes: () => ({\n class: 'dou-editor__surface',\n role: 'textbox',\n 'aria-multiline': 'true',\n ...options.attributes?.(),\n }),\n // `this`, not `view`: plugins (e.g. collaboration) dispatch while the view is being created\n dispatchTransaction(this: EditorView, tr) {\n const before = this.state\n const next = before.apply(tr)\n this.updateState(next)\n if (next.doc !== before.doc) options.onChange?.(this, tr, before)\n options.onUpdate?.(this, before)\n },\n })\n viewNodeViews.set(view, nodeViews)\n return view\n}\n\nexport function setEditable(view: EditorView, value: boolean) {\n // node views draw controls (checkboxes…) that are disabled while read-only\n view.setProps({ editable: () => value, nodeViews: fresh(viewNodeViews.get(view) ?? {}) })\n}\n\n/** Replace the whole document, e.g. when `v-model` changes from outside. */\nexport function replaceBlocks(view: EditorView, blocks: PartialBlock[]) {\n const state = view.state\n const doc = blocksToDoc(state.schema, blocks)\n const tr = state.tr.replaceWith(0, state.doc.content.size, doc.content)\n tr.setMeta('addToHistory', false)\n tr.setMeta(originKey, 'external')\n view.dispatch(tr)\n}\n\n/**\n * Replace the document with `blocks` as one change undo takes back, e.g.\n * an older version. Only what differs is replaced, so the blocks that stay\n * keep their place, and with collaboration only they are written.\n */\nexport function restoreBlocks(view: EditorView, blocks: PartialBlock[]) {\n const state = view.state\n const next = blocksToDoc(state.schema, blocks)\n const start = state.doc.content.findDiffStart(next.content)\n if (start === null) return false\n const end = state.doc.content.findDiffEnd(next.content)!\n // the two ends can cross where the same text repeats: widen them to agree\n const overlap = start - Math.min(end.a, end.b)\n if (overlap > 0) {\n end.a += overlap\n end.b += overlap\n }\n view.dispatch(\n state.tr\n .replace(start, end.a, next.slice(start, end.b))\n .setMeta(originKey, 'restore')\n .scrollIntoView(),\n )\n return true\n}\n\nexport function getBlocks(view: EditorView): Block[] {\n return docToBlocks(view.state.doc)\n}\n\nexport function runCommand(view: EditorView, command: Command): boolean {\n return command(view.state, view.dispatch, view)\n}\n\nexport type { PMNode }\n"],"mappings":";;;;;;;;;;;;;AA+CA,IAAa,YAAY;AAEzB,IAAM,aAAa,IAAI,UAA4C,YAAY;;;;;;AAO/E,SAAgB,gBAAgB,OAG9B;CACA,OAAO,WAAW,SAAS,KAAK,KAAK;EAAE;EAAM;CAAK;AACpD;AAEA,SAAgB,cAAc,SAAyD;CACrF,MAAM,EAAE,QAAQ;CAChB,MAAM,WAAW,IAAI;CACrB,MAAM,iBAAiB,QAAQ,YAAY,SAAS,CAAC;CACrD,MAAM,WAAW;EACf,MAAM,QAAQ,QAAQ,UAAU,QAAQ;EACxC,MAAM,QAAQ,QAAQ,UAAU,QAAQ;CAC1C;CACA,OAAO;EACL,IAAI,OAAO;GACT,KAAK;GACL,OAAO;IAAE,YAAY;IAAU,QAAQ,KAAK,UAAU;GAAM;EAC9D,CAAC;EACD,GAAI,QAAQ,WAAW,CAAC;EACxB,GAAG,IAAI;EACP,iBAAiB,IAAI,UAAU;EAC/B,qBAAqB;EACrB,GAAG,IAAI,QAAQ,KAAK,aAAa,OAAO,QAAQ,CAAC;EACjD,GAAI,QAAQ,WAAW,CAAC;EACxB,aAAa,QAAQ;EACrB,OAAO,UAAU;EACjB,GAAI,UAAU,WAAW,CAAC;EAC1B,GAAI,iBAAiB,CAAC,QAAQ,CAAC,IAAI,CAAC;EACpC,mBAAmB,GAAG;EACtB,SAAS;EACT,WAAW;EACX,kBAAkB,QAAQ,WAAW;CACvC;AACF;AAEA,SAAgB,YACd,KACA,QACA,SACa;CACb,OAAO,YAAY,OAAO;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,IAAI,OAAO;CACtE,GAAG,QAAQ,gBAAgB,KAAK;CAChC,GAAG,QAAQ,WAAW,UAAU;CAChC,KAAK,SAAS,EAAE;AAClB;;;;;;AAOA,SAAgB,cAAc,MAAkB,QAAwB;CACtE,MAAM,QAAQ,KAAK;CACnB,MAAM,OAAO,YAAY,MAAM,QAAQ,MAAM;CAC7C,MAAM,QAAQ,MAAM,IAAI,QAAQ,cAAc,KAAK,OAAO;CAC1D,IAAI,UAAU,MAAM,OAAO;CAC3B,MAAM,MAAM,MAAM,IAAI,QAAQ,YAAY,KAAK,OAAO;CAEtD,MAAM,UAAU,QAAQ,KAAK,IAAI,IAAI,GAAG,IAAI,CAAC;CAC7C,IAAI,UAAU,GAAG;EACf,IAAI,KAAK;EACT,IAAI,KAAK;CACX;CACA,KAAK,SACH,MAAM,GACH,QAAQ,OAAO,IAAI,GAAG,KAAK,MAAM,OAAO,IAAI,CAAC,CAAC,CAAC,CAC/C,QAAQ,WAAW,SAAS,CAAC,CAC7B,eAAe,CACpB;CACA,OAAO;AACT;AAEA,SAAgB,UAAU,MAA2B;CACnD,OAAO,YAAY,KAAK,MAAM,GAAG;AACnC;AAEA,SAAgB,WAAW,MAAkB,SAA2B;CACtE,OAAO,QAAQ,KAAK,OAAO,KAAK,UAAU,IAAI;AAChD"}
|
package/dist/core/plugins.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createId } from "./model.js";
|
|
2
2
|
import { Plugin, PluginKey } from "prosemirror-state";
|
|
3
3
|
import { Decoration, DecorationSet } from "prosemirror-view";
|
|
4
|
+
import { ReplaceAroundStep, ReplaceStep } from "prosemirror-transform";
|
|
4
5
|
// src/core/plugins.ts
|
|
5
6
|
/**
|
|
6
7
|
* Whether a transaction came from another collaborator. Collaborative
|
|
@@ -38,6 +39,111 @@ function idPlugin() {
|
|
|
38
39
|
}
|
|
39
40
|
});
|
|
40
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Whether a transaction may have changed which blocks there are, their
|
|
44
|
+
* types or their nesting. Typing and deleting inside one text block, marks
|
|
45
|
+
* and attributes don't; decorations that only follow the block structure
|
|
46
|
+
* then just map through the change instead of being rebuilt.
|
|
47
|
+
*/
|
|
48
|
+
function changesBlockStructure(tr) {
|
|
49
|
+
if (!tr.docChanged) return false;
|
|
50
|
+
return tr.steps.some((step, index) => {
|
|
51
|
+
if (step instanceof ReplaceAroundStep) return true;
|
|
52
|
+
if (!(step instanceof ReplaceStep)) return false;
|
|
53
|
+
const { from, to, slice } = step;
|
|
54
|
+
if (slice.openStart || slice.openEnd) return true;
|
|
55
|
+
let blocks = false;
|
|
56
|
+
slice.content.forEach((node) => {
|
|
57
|
+
if (!node.isInline) blocks = true;
|
|
58
|
+
});
|
|
59
|
+
if (blocks) return true;
|
|
60
|
+
const doc = tr.docs[index];
|
|
61
|
+
const $from = doc.resolve(from);
|
|
62
|
+
return !$from.parent.isTextblock || !$from.sameParent(doc.resolve(to));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** Plugin state for decorations that only depend on the block structure. */
|
|
66
|
+
var structureDecorations = (build, update = (_set, tr) => build(tr.doc)) => ({
|
|
67
|
+
init: (_config, state) => build(state.doc),
|
|
68
|
+
apply: (tr, value) => !tr.docChanged ? value : changesBlockStructure(tr) ? update(value, tr) : value.map(tr.mapping, tr.doc)
|
|
69
|
+
});
|
|
70
|
+
var isList = (block) => !!block?.firstChild?.type.spec.continues;
|
|
71
|
+
/** Whether a sibling block is an item of the same list as `block`. */
|
|
72
|
+
var sameList = (sibling, block) => isList(sibling) && sibling.firstChild.type === block.firstChild.type;
|
|
73
|
+
/**
|
|
74
|
+
* The content of blocks that continue like a list (`continues` in their
|
|
75
|
+
* spec) gets `is-list`, so the stylesheet can space list items apart by the list gap
|
|
76
|
+
* instead of the block gap. The first and last block of a run of them also
|
|
77
|
+
* get `is-list-start` / `is-list-end`, where the block gap comes back. A run
|
|
78
|
+
* is one type: a to-do list right after a numbered one is a list of its
|
|
79
|
+
* own. Nested items under a list item, of any type, carry on its run.
|
|
80
|
+
*/
|
|
81
|
+
/** The list decorations of top-level blocks `first` to `final` (and their children). */
|
|
82
|
+
function listDecorationsOf(doc, first, final) {
|
|
83
|
+
const decorations = [];
|
|
84
|
+
const visit = (container, start, inList, from = 0, to = Infinity) => {
|
|
85
|
+
const last = container.childCount - 1;
|
|
86
|
+
container.forEach((block, offset, index) => {
|
|
87
|
+
if (index < from || index > to) return;
|
|
88
|
+
const pos = start + offset;
|
|
89
|
+
const content = block.firstChild;
|
|
90
|
+
const groupPos = pos + 1 + content.nodeSize;
|
|
91
|
+
const list = isList(block);
|
|
92
|
+
if (list) {
|
|
93
|
+
const classes = ["is-list"];
|
|
94
|
+
const previous = index > 0 ? container.child(index - 1) : null;
|
|
95
|
+
if (!(previous ? sameList(previous, block) : inList)) {
|
|
96
|
+
classes.push("is-list-start");
|
|
97
|
+
if (previous && isList(previous)) classes.push("is-list-after");
|
|
98
|
+
}
|
|
99
|
+
const end = !(index < last ? sameList(container.child(index + 1), block) : inList);
|
|
100
|
+
if (end && block.childCount < 2) classes.push("is-list-end");
|
|
101
|
+
decorations.push(Decoration.node(pos + 1, groupPos, { class: classes.join(" ") }));
|
|
102
|
+
if (end && block.childCount > 1) decorations.push(Decoration.node(groupPos, groupPos + block.child(1).nodeSize, { class: "is-list-end" }));
|
|
103
|
+
}
|
|
104
|
+
if (block.childCount > 1) visit(block.child(1), groupPos + 1, list);
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
visit(doc, 0, false, first, final);
|
|
108
|
+
return decorations;
|
|
109
|
+
}
|
|
110
|
+
var listDecorations = (doc) => DecorationSet.create(doc, listDecorationsOf(doc, 0, doc.childCount - 1));
|
|
111
|
+
/**
|
|
112
|
+
* After blocks were added, removed or changed type: only the top-level
|
|
113
|
+
* blocks the change touched, and one on either side (whose run may have
|
|
114
|
+
* started or ended there), are worked out again.
|
|
115
|
+
*/
|
|
116
|
+
function updateListDecorations(set, tr) {
|
|
117
|
+
const { doc, mapping } = tr;
|
|
118
|
+
let from = doc.content.size;
|
|
119
|
+
let to = 0;
|
|
120
|
+
mapping.maps.forEach((map, index) => {
|
|
121
|
+
const rest = mapping.slice(index + 1);
|
|
122
|
+
map.forEach((_oldStart, _oldEnd, start, end) => {
|
|
123
|
+
from = Math.min(from, rest.map(start, -1));
|
|
124
|
+
to = Math.max(to, rest.map(end, 1));
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
const mapped = set.map(mapping, doc);
|
|
128
|
+
if (from > to) return mapped;
|
|
129
|
+
const first = Math.max(0, doc.resolve(from).index(0) - 1);
|
|
130
|
+
const last = Math.min(doc.childCount - 1, doc.resolve(to).index(0) + 1);
|
|
131
|
+
let start = 0;
|
|
132
|
+
for (let index = 0; index < first; index++) start += doc.child(index).nodeSize;
|
|
133
|
+
let end = start;
|
|
134
|
+
for (let index = first; index <= last; index++) end += doc.child(index).nodeSize;
|
|
135
|
+
return mapped.remove(mapped.find(start, end)).add(doc, listDecorationsOf(doc, first, last));
|
|
136
|
+
}
|
|
137
|
+
/** Marks list-like blocks and the ends of their runs, for spacing. */
|
|
138
|
+
function listPlugin() {
|
|
139
|
+
return new Plugin({
|
|
140
|
+
key: new PluginKey("dou-editor-lists"),
|
|
141
|
+
state: structureDecorations(listDecorations, updateListDecorations),
|
|
142
|
+
props: { decorations(state) {
|
|
143
|
+
return this.getState(state);
|
|
144
|
+
} }
|
|
145
|
+
});
|
|
146
|
+
}
|
|
41
147
|
/** Numbered items count up within each run of consecutive numbered siblings. */
|
|
42
148
|
function numberDecorations(doc) {
|
|
43
149
|
const decorations = [];
|
|
@@ -59,10 +165,7 @@ function numberDecorations(doc) {
|
|
|
59
165
|
function numberingPlugin() {
|
|
60
166
|
return new Plugin({
|
|
61
167
|
key: new PluginKey("dou-editor-numbering"),
|
|
62
|
-
state:
|
|
63
|
-
init: (_config, state) => numberDecorations(state.doc),
|
|
64
|
-
apply: (tr, value) => tr.docChanged ? numberDecorations(tr.doc) : value
|
|
65
|
-
},
|
|
168
|
+
state: structureDecorations(numberDecorations),
|
|
66
169
|
props: { decorations(state) {
|
|
67
170
|
return this.getState(state);
|
|
68
171
|
} }
|
|
@@ -97,6 +200,6 @@ function placeholderPlugin(options) {
|
|
|
97
200
|
} }
|
|
98
201
|
});
|
|
99
202
|
}
|
|
100
|
-
export { idPlugin, isRemote, numberingPlugin, placeholderPlugin };
|
|
203
|
+
export { changesBlockStructure, idPlugin, isRemote, listPlugin, numberingPlugin, placeholderPlugin };
|
|
101
204
|
|
|
102
205
|
//# sourceMappingURL=plugins.js.map
|
package/dist/core/plugins.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugins.js","names":[],"sources":["../../src/core/plugins.ts"],"sourcesContent":["import type { Node as PMNode } from 'prosemirror-model'\nimport { Plugin, PluginKey, type Transaction } from 'prosemirror-state'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\nimport { createId } from './model'\n\n/**\n * Whether a transaction came from another collaborator. Collaborative\n * bindings mark theirs; the Yjs binding uses the `y-sync$` key.\n */\nexport function isRemote(tr: Transaction): boolean {\n const meta = tr.getMeta('y-sync$') as { isChangeOrigin?: boolean } | undefined\n return !!meta?.isChangeOrigin || tr.getMeta('remote') === true\n}\n\n/**\n * Every block gets a unique id. Splitting a block copies its attributes,\n * so the copy gets a fresh id here; the first occurrence keeps the old one.\n * Changes from collaborators are left alone, so two editors never fight\n * over an id.\n */\nexport function idPlugin(): Plugin {\n return new Plugin({\n key: new PluginKey('dou-editor-ids'),\n appendTransaction(transactions, _old, state) {\n if (!transactions.some((tr) => tr.docChanged) || transactions.every(isRemote)) return null\n const seen = new Set<string>()\n let tr: Transaction | null = null\n state.doc.descendants((node, pos) => {\n if (node.type.name !== 'block') return node.type.name === 'blockGroup'\n const id = node.attrs.id as string | null\n if (!id || seen.has(id)) {\n tr ??= state.tr\n const fresh = createId()\n tr.setNodeAttribute(pos, 'id', fresh)\n seen.add(fresh)\n } else seen.add(id)\n return true\n })\n if (tr) (tr as Transaction).setMeta('addToHistory', false)\n return tr\n },\n })\n}\n\n/** Numbered items count up within each run of consecutive numbered siblings. */\nfunction numberDecorations(doc: PMNode): DecorationSet {\n const decorations: Decoration[] = []\n const visit = (container: PMNode, start: number) => {\n let count = 0\n container.forEach((block, offset) => {\n const pos = start + offset\n const content = block.firstChild\n if (content?.type.name === 'numberedItem') {\n count++\n decorations.push(\n Decoration.node(pos + 1, pos + 1 + content.nodeSize, { 'data-index': String(count) }),\n )\n } else count = 0\n if (block.childCount > 1) visit(block.child(1), pos + 1 + content!.nodeSize + 1)\n })\n }\n visit(doc, 0)\n return DecorationSet.create(doc, decorations)\n}\n\nexport function numberingPlugin(): Plugin {\n return new Plugin<DecorationSet>({\n key: new PluginKey('dou-editor-numbering'),\n state: {\n init: (_config, state) => numberDecorations(state.doc),\n apply: (tr, value) => (tr.docChanged ? numberDecorations(tr.doc) : value),\n },\n props: {\n decorations(state) {\n return this.getState(state)\n },\n },\n })\n}\n\nexport interface PlaceholderOptions {\n /** The hint in the empty block the cursor is in. */\n text: () => string\n /** The hint in an empty heading. */\n heading: () => string\n /** The hint when the whole document is one empty paragraph. */\n empty: () => string\n}\n\n/** A hint inside the empty block under the cursor, and in an empty document. */\nexport function placeholderPlugin(options: PlaceholderOptions): Plugin {\n return new Plugin({\n key: new PluginKey('dou-editor-placeholder'),\n props: {\n decorations(state) {\n const { doc, selection } = state\n const decorations: Decoration[] = []\n const onlyBlock =\n doc.childCount === 1 && doc.firstChild!.childCount === 1 && doc.firstChild!.firstChild!\n if (onlyBlock && onlyBlock.type.name === 'paragraph' && onlyBlock.content.size === 0) {\n decorations.push(\n Decoration.node(1, 1 + onlyBlock.nodeSize, {\n class: 'is-empty',\n 'data-placeholder': options.empty(),\n }),\n )\n return DecorationSet.create(doc, decorations)\n }\n const { $from } = selection\n const parent = $from.parent\n if (!selection.empty || !parent.isTextblock || parent.content.size > 0) return null\n if (parent.type.spec.code) return null\n const text = parent.type.name === 'heading' ? options.heading() : options.text()\n const start = $from.before()\n decorations.push(\n Decoration.node(start, start + parent.nodeSize, {\n class: 'is-empty',\n 'data-placeholder': text,\n }),\n )\n return DecorationSet.create(doc, decorations)\n },\n },\n })\n}\n"],"mappings":";;;;;;;;AASA,SAAgB,SAAS,IAA0B;CAEjD,OAAO,CAAC,CADK,GAAG,QAAQ,SACf,CAAA,EAAM,kBAAkB,GAAG,QAAQ,QAAQ,MAAM;AAC5D;;;;;;;AAQA,SAAgB,WAAmB;CACjC,OAAO,IAAI,OAAO;EAChB,KAAK,IAAI,UAAU,gBAAgB;EACnC,kBAAkB,cAAc,MAAM,OAAO;GAC3C,IAAI,CAAC,aAAa,MAAM,OAAO,GAAG,UAAU,KAAK,aAAa,MAAM,QAAQ,GAAG,OAAO;GACtF,MAAM,uBAAO,IAAI,IAAY;GAC7B,IAAI,KAAyB;GAC7B,MAAM,IAAI,aAAa,MAAM,QAAQ;IACnC,IAAI,KAAK,KAAK,SAAS,SAAS,OAAO,KAAK,KAAK,SAAS;IAC1D,MAAM,KAAK,KAAK,MAAM;IACtB,IAAI,CAAC,MAAM,KAAK,IAAI,EAAE,GAAG;KACvB,OAAO,MAAM;KACb,MAAM,QAAQ,SAAS;KACvB,GAAG,iBAAiB,KAAK,MAAM,KAAK;KACpC,KAAK,IAAI,KAAK;IAChB,OAAO,KAAK,IAAI,EAAE;IAClB,OAAO;GACT,CAAC;GACD,IAAI,IAAI,GAAoB,QAAQ,gBAAgB,KAAK;GACzD,OAAO;EACT;CACF,CAAC;AACH;;AAGA,SAAS,kBAAkB,KAA4B;CACrD,MAAM,cAA4B,CAAC;CACnC,MAAM,SAAS,WAAmB,UAAkB;EAClD,IAAI,QAAQ;EACZ,UAAU,SAAS,OAAO,WAAW;GACnC,MAAM,MAAM,QAAQ;GACpB,MAAM,UAAU,MAAM;GACtB,IAAI,SAAS,KAAK,SAAS,gBAAgB;IACzC;IACA,YAAY,KACV,WAAW,KAAK,MAAM,G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AQ,QAAQ,IAAI,QAAQ,KAAK;GAC/E,MAAM,QAAQ,MAAM,OAAO;GAC3B,YAAY,KACV,WAAW,KAAK,OAAO,QAAQ,OAAO,UAAU;IAC9C,OAAO;IACP,oBAAoB;GACtB,CAAC,CACH;GACA,OAAO,cAAc,OAAO,KAAK,WAAW;EAC9C,EACF;CACF,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"plugins.js","names":[],"sources":["../../src/core/plugins.ts"],"sourcesContent":["import type { Node as PMNode } from 'prosemirror-model'\nimport { Plugin, PluginKey, type Transaction } from 'prosemirror-state'\nimport { ReplaceAroundStep, ReplaceStep } from 'prosemirror-transform'\nimport { Decoration, DecorationSet } from 'prosemirror-view'\nimport { createId } from './model'\n\n/**\n * Whether a transaction came from another collaborator. Collaborative\n * bindings mark theirs; the Yjs binding uses the `y-sync$` key.\n */\nexport function isRemote(tr: Transaction): boolean {\n const meta = tr.getMeta('y-sync$') as { isChangeOrigin?: boolean } | undefined\n return !!meta?.isChangeOrigin || tr.getMeta('remote') === true\n}\n\n/**\n * Every block gets a unique id. Splitting a block copies its attributes,\n * so the copy gets a fresh id here; the first occurrence keeps the old one.\n * Changes from collaborators are left alone, so two editors never fight\n * over an id.\n */\nexport function idPlugin(): Plugin {\n return new Plugin({\n key: new PluginKey('dou-editor-ids'),\n appendTransaction(transactions, _old, state) {\n if (!transactions.some((tr) => tr.docChanged) || transactions.every(isRemote)) return null\n const seen = new Set<string>()\n let tr: Transaction | null = null\n state.doc.descendants((node, pos) => {\n if (node.type.name !== 'block') return node.type.name === 'blockGroup'\n const id = node.attrs.id as string | null\n if (!id || seen.has(id)) {\n tr ??= state.tr\n const fresh = createId()\n tr.setNodeAttribute(pos, 'id', fresh)\n seen.add(fresh)\n } else seen.add(id)\n return true\n })\n if (tr) (tr as Transaction).setMeta('addToHistory', false)\n return tr\n },\n })\n}\n\n/**\n * Whether a transaction may have changed which blocks there are, their\n * types or their nesting. Typing and deleting inside one text block, marks\n * and attributes don't; decorations that only follow the block structure\n * then just map through the change instead of being rebuilt.\n */\nexport function changesBlockStructure(tr: Transaction): boolean {\n if (!tr.docChanged) return false\n return tr.steps.some((step, index) => {\n if (step instanceof ReplaceAroundStep) return true\n if (!(step instanceof ReplaceStep)) return false\n const { from, to, slice } = step\n if (slice.openStart || slice.openEnd) return true\n let blocks = false\n slice.content.forEach((node) => {\n if (!node.isInline) blocks = true\n })\n if (blocks) return true\n const doc = tr.docs[index]!\n const $from = doc.resolve(from)\n return !$from.parent.isTextblock || !$from.sameParent(doc.resolve(to))\n })\n}\n\n/** Plugin state for decorations that only depend on the block structure. */\nconst structureDecorations = (\n build: (doc: PMNode) => DecorationSet,\n update: (set: DecorationSet, tr: Transaction) => DecorationSet = (_set, tr) => build(tr.doc),\n) => ({\n init: (_config: unknown, state: { doc: PMNode }) => build(state.doc),\n apply: (tr: Transaction, value: DecorationSet) =>\n !tr.docChanged\n ? value\n : changesBlockStructure(tr)\n ? update(value, tr)\n : value.map(tr.mapping, tr.doc),\n})\n\nconst isList = (block: PMNode | null) => !!block?.firstChild?.type.spec.continues\n\n/** Whether a sibling block is an item of the same list as `block`. */\nconst sameList = (sibling: PMNode, block: PMNode) =>\n isList(sibling) && sibling.firstChild!.type === block.firstChild!.type\n\n/**\n * The content of blocks that continue like a list (`continues` in their\n * spec) gets `is-list`, so the stylesheet can space list items apart by the list gap\n * instead of the block gap. The first and last block of a run of them also\n * get `is-list-start` / `is-list-end`, where the block gap comes back. A run\n * is one type: a to-do list right after a numbered one is a list of its\n * own. Nested items under a list item, of any type, carry on its run.\n */\n/** The list decorations of top-level blocks `first` to `final` (and their children). */\nfunction listDecorationsOf(doc: PMNode, first: number, final: number): Decoration[] {\n const decorations: Decoration[] = []\n const visit = (container: PMNode, start: number, inList: boolean, from = 0, to = Infinity) => {\n const last = container.childCount - 1\n container.forEach((block, offset, index) => {\n if (index < from || index > to) return\n const pos = start + offset\n const content = block.firstChild!\n const groupPos = pos + 1 + content.nodeSize\n const list = isList(block)\n if (list) {\n // all on the content (or the children's group), never on the block\n // itself: the view looks up a block's own decorations by scanning\n // all of its parent's, and a selector matching blocks by what they\n // hold would be checked again for every block when blocks change\n const classes = ['is-list']\n const previous = index > 0 ? container.child(index - 1) : null\n if (!(previous ? sameList(previous, block) : inList)) {\n classes.push('is-list-start')\n // right after another list: the two edges overlap\n if (previous && isList(previous)) classes.push('is-list-after')\n }\n const end = !(index < last ? sameList(container.child(index + 1), block) : inList)\n if (end && block.childCount < 2) classes.push('is-list-end')\n decorations.push(Decoration.node(pos + 1, groupPos, { class: classes.join(' ') }))\n if (end && block.childCount > 1)\n decorations.push(\n Decoration.node(groupPos, groupPos + block.child(1).nodeSize, { class: 'is-list-end' }),\n )\n }\n if (block.childCount > 1) visit(block.child(1), groupPos + 1, list)\n })\n }\n visit(doc, 0, false, first, final)\n return decorations\n}\n\nconst listDecorations = (doc: PMNode) =>\n DecorationSet.create(doc, listDecorationsOf(doc, 0, doc.childCount - 1))\n\n/**\n * After blocks were added, removed or changed type: only the top-level\n * blocks the change touched, and one on either side (whose run may have\n * started or ended there), are worked out again.\n */\nfunction updateListDecorations(set: DecorationSet, tr: Transaction): DecorationSet {\n const { doc, mapping } = tr\n let from = doc.content.size\n let to = 0\n mapping.maps.forEach((map, index) => {\n const rest = mapping.slice(index + 1)\n map.forEach((_oldStart, _oldEnd, start, end) => {\n from = Math.min(from, rest.map(start, -1))\n to = Math.max(to, rest.map(end, 1))\n })\n })\n const mapped = set.map(mapping, doc)\n if (from > to) return mapped\n const first = Math.max(0, doc.resolve(from).index(0) - 1)\n const last = Math.min(doc.childCount - 1, doc.resolve(to).index(0) + 1)\n let start = 0\n for (let index = 0; index < first; index++) start += doc.child(index).nodeSize\n let end = start\n for (let index = first; index <= last; index++) end += doc.child(index).nodeSize\n return mapped.remove(mapped.find(start, end)).add(doc, listDecorationsOf(doc, first, last))\n}\n\n/** Marks list-like blocks and the ends of their runs, for spacing. */\nexport function listPlugin(): Plugin {\n return new Plugin<DecorationSet>({\n key: new PluginKey('dou-editor-lists'),\n state: structureDecorations(listDecorations, updateListDecorations),\n props: {\n decorations(state) {\n return this.getState(state)\n },\n },\n })\n}\n\n/** Numbered items count up within each run of consecutive numbered siblings. */\nfunction numberDecorations(doc: PMNode): DecorationSet {\n const decorations: Decoration[] = []\n const visit = (container: PMNode, start: number) => {\n let count = 0\n container.forEach((block, offset) => {\n const pos = start + offset\n const content = block.firstChild\n if (content?.type.name === 'numberedItem') {\n count++\n decorations.push(\n Decoration.node(pos + 1, pos + 1 + content.nodeSize, { 'data-index': String(count) }),\n )\n } else count = 0\n if (block.childCount > 1) visit(block.child(1), pos + 1 + content!.nodeSize + 1)\n })\n }\n visit(doc, 0)\n return DecorationSet.create(doc, decorations)\n}\n\nexport function numberingPlugin(): Plugin {\n return new Plugin<DecorationSet>({\n key: new PluginKey('dou-editor-numbering'),\n state: structureDecorations(numberDecorations),\n props: {\n decorations(state) {\n return this.getState(state)\n },\n },\n })\n}\n\nexport interface PlaceholderOptions {\n /** The hint in the empty block the cursor is in. */\n text: () => string\n /** The hint in an empty heading. */\n heading: () => string\n /** The hint when the whole document is one empty paragraph. */\n empty: () => string\n}\n\n/** A hint inside the empty block under the cursor, and in an empty document. */\nexport function placeholderPlugin(options: PlaceholderOptions): Plugin {\n return new Plugin({\n key: new PluginKey('dou-editor-placeholder'),\n props: {\n decorations(state) {\n const { doc, selection } = state\n const decorations: Decoration[] = []\n const onlyBlock =\n doc.childCount === 1 && doc.firstChild!.childCount === 1 && doc.firstChild!.firstChild!\n if (onlyBlock && onlyBlock.type.name === 'paragraph' && onlyBlock.content.size === 0) {\n decorations.push(\n Decoration.node(1, 1 + onlyBlock.nodeSize, {\n class: 'is-empty',\n 'data-placeholder': options.empty(),\n }),\n )\n return DecorationSet.create(doc, decorations)\n }\n const { $from } = selection\n const parent = $from.parent\n if (!selection.empty || !parent.isTextblock || parent.content.size > 0) return null\n if (parent.type.spec.code) return null\n const text = parent.type.name === 'heading' ? options.heading() : options.text()\n const start = $from.before()\n decorations.push(\n Decoration.node(start, start + parent.nodeSize, {\n class: 'is-empty',\n 'data-placeholder': text,\n }),\n )\n return DecorationSet.create(doc, decorations)\n },\n },\n })\n}\n"],"mappings":";;;;;;;;;AAUA,SAAgB,SAAS,IAA0B;CAEjD,OAAO,CAAC,CADK,GAAG,QAAQ,SACf,CAAA,EAAM,kBAAkB,GAAG,QAAQ,QAAQ,MAAM;AAC5D;;;;;;;AAQA,SAAgB,WAAmB;CACjC,OAAO,IAAI,OAAO;EAChB,KAAK,IAAI,UAAU,gBAAgB;EACnC,kBAAkB,cAAc,MAAM,OAAO;GAC3C,IAAI,CAAC,aAAa,MAAM,OAAO,GAAG,UAAU,KAAK,aAAa,MAAM,QAAQ,GAAG,OAAO;GACtF,MAAM,uBAAO,IAAI,IAAY;GAC7B,IAAI,KAAyB;GAC7B,MAAM,IAAI,aAAa,MAAM,QAAQ;IACnC,IAAI,KAAK,KAAK,SAAS,SAAS,OAAO,KAAK,KAAK,SAAS;IAC1D,MAAM,KAAK,KAAK,MAAM;IACtB,IAAI,CAAC,MAAM,KAAK,IAAI,EAAE,GAAG;KACvB,OAAO,MAAM;KACb,MAAM,QAAQ,SAAS;KACvB,GAAG,iBAAiB,KAAK,MAAM,KAAK;KACpC,KAAK,IAAI,KAAK;IAChB,OAAO,KAAK,IAAI,EAAE;IAClB,OAAO;GACT,CAAC;GACD,IAAI,IAAI,GAAoB,QAAQ,gBAAgB,KAAK;GACzD,OAAO;EACT;CACF,CAAC;AACH;;;;;;;AAQA,SAAgB,sBAAsB,IAA0B;CAC9D,IAAI,CAAC,GAAG,YAAY,OAAO;CAC3B,OAAO,GAAG,MAAM,MAAM,MAAM,UAAU;EACpC,IAAI,gBAAgB,mBAAmB,OAAO;EAC9C,IAAI,EAAE,gBAAgB,cAAc,OAAO;EAC3C,MAAM,EAAE,MAAM,IAAI,UAAU;EAC5B,IAAI,MAAM,aAAa,MAAM,SAAS,OAAO;EAC7C,IAAI,SAAS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U,sBAAsB;EACzC,OAAO,qBAAqB,iBAAiB;EAC7C,OAAO,EACL,YAAY,OAAO;GACjB,OAAO,KAAK,SAAS,KAAK;EAC5B,EACF;CACF,CAAC;AACH;;AAYA,SAAgB,kBAAkB,SAAqC;CACrE,OAAO,IAAI,OAAO;EAChB,KAAK,IAAI,UAAU,wBAAwB;EAC3C,OAAO,EACL,YAAY,OAAO;GACjB,MAAM,EAAE,KAAK,cAAc;GAC3B,MAAM,cAA4B,CAAC;GACnC,MAAM,YACJ,IAAI,eAAe,KAAK,IAAI,WAAY,eAAe,KAAK,IAAI,WAAY;GAC9E,IAAI,aAAa,UAAU,KAAK,SAAS,eAAe,UAAU,QAAQ,SAAS,GAAG;IACpF,YAAY,KACV,WAAW,KAAK,GAAG,IAAI,UAAU,UAAU;KACzC,OAAO;KACP,oBAAoB,QAAQ,MAAM;IACpC,CAAC,CACH;IACA,OAAO,cAAc,OAAO,KAAK,WAAW;GAC9C;GACA,MAAM,EAAE,UAAU;GAClB,MAAM,SAAS,MAAM;GACrB,IAAI,CAAC,UAAU,SAAS,CAAC,OAAO,eAAe,OAAO,QAAQ,OAAO,GAAG,OAAO;GAC/E,IAAI,OAAO,KAAK,KAAK,MAAM,OAAO;GAClC,MAAM,OAAO,OAAO,KAAK,SAAS,YAAY,QAAQ,QAAQ,IAAI,QAAQ,KAAK;GAC/E,MAAM,QAAQ,MAAM,OAAO;GAC3B,YAAY,KACV,WAAW,KAAK,OAAO,QAAQ,OAAO,UAAU;IAC9C,OAAO;IACP,oBAAoB;GACtB,CAAC,CACH;GACA,OAAO,cAAc,OAAO,KAAK,WAAW;EAC9C,EACF;CACF,CAAC;AACH"}
|
package/dist/dom.js
CHANGED
|
@@ -43,6 +43,11 @@ function firstLine(content) {
|
|
|
43
43
|
height
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
/** The right edge of the editor's text column: the surface less its padding (a reading width). */
|
|
47
|
+
function columnRight(surface) {
|
|
48
|
+
const padding = Number.parseFloat(getComputedStyle(surface).paddingRight) || 0;
|
|
49
|
+
return surface.getBoundingClientRect().right - padding;
|
|
50
|
+
}
|
|
51
|
+
export { blockElementOf, columnRight, contentAt, firstLine, scrollParent };
|
|
47
52
|
|
|
48
53
|
//# sourceMappingURL=dom.js.map
|
package/dist/dom.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dom.js","names":[],"sources":["../src/dom.ts"],"sourcesContent":["import type { EditorView } from 'prosemirror-view'\n\n/**\n * The content element (paragraph, heading, …) at height `y` in viewport\n * coordinates: the last one whose top is above it. Contents follow each\n * other from top to bottom in document order, nested ones included, so\n * a binary search does.\n */\nexport function contentAt(view: EditorView, y: number): HTMLElement | null {\n const list = view.dom.querySelectorAll<HTMLElement>('.dou-editor__content')\n let low = 0\n let high = list.length - 1\n let found = -1\n while (low <= high) {\n const middle = (low + high) >> 1\n if (list[middle]!.getBoundingClientRect().top <= y) {\n found = middle\n low = middle + 1\n } else high = middle - 1\n }\n return list[Math.max(found, 0)] ?? null\n}\n\n/** The block element around a content element. */\nexport function blockElementOf(content: HTMLElement): HTMLElement | null {\n return content.closest<HTMLElement>('.dou-editor__block')\n}\n\n/** The nearest ancestor that scrolls vertically, or `null` for the page. */\nexport function scrollParent(element: HTMLElement): HTMLElement | null {\n for (let node = element.parentElement; node; node = node.parentElement) {\n const { overflowY } = getComputedStyle(node)\n if ((overflowY === 'auto' || overflowY === 'scroll') && node.scrollHeight > node.clientHeight)\n return node\n }\n return null\n}\n\n/** Where the first line of a content element sits: its top and height. */\nexport function firstLine(content: HTMLElement): { top: number; height: number } {\n const rect = content.getBoundingClientRect()\n const style = getComputedStyle(content)\n const lineHeight = Number.parseFloat(style.lineHeight)\n const padding = Number.parseFloat(style.paddingTop) || 0\n const height = Number.isFinite(lineHeight) ? Math.min(lineHeight, rect.height) : rect.height\n return { top: rect.top + (height < rect.height ? padding : 0), height }\n}\n"],"mappings":";;;;;;;AAQA,SAAgB,UAAU,MAAkB,GAA+B;CACzE,MAAM,OAAO,KAAK,IAAI,iBAA8B,sBAAsB;CAC1E,IAAI,MAAM;CACV,IAAI,OAAO,KAAK,SAAS;CACzB,IAAI,QAAQ;CACZ,OAAO,OAAO,MAAM;EAClB,MAAM,SAAU,MAAM,QAAS;EAC/B,IAAI,KAAK,OAAO,CAAE,sBAAsB,CAAC,CAAC,OAAO,GAAG;GAClD,QAAQ;GACR,MAAM,SAAS;EACjB,OAAO,OAAO,SAAS;CACzB;CACA,OAAO,KAAK,KAAK,IAAI,OAAO,CAAC,MAAM;AACrC;;AAGA,SAAgB,eAAe,SAA0C;CACvE,OAAO,QAAQ,QAAqB,oBAAoB;AAC1D;;AAGA,SAAgB,aAAa,SAA0C;CACrE,KAAK,IAAI,OAAO,QAAQ,eAAe,MAAM,OAAO,KAAK,eAAe;EACtE,MAAM,EAAE,cAAc,iBAAiB,IAAI;EAC3C,KAAK,cAAc,UAAU,cAAc,aAAa,KAAK,eAAe,KAAK,cAC/E,OAAO;CACX;CACA,OAAO;AACT;;AAGA,SAAgB,UAAU,SAAuD;CAC/E,MAAM,OAAO,QAAQ,sBAAsB;CAC3C,MAAM,QAAQ,iBAAiB,OAAO;CACtC,MAAM,aAAa,OAAO,WAAW,MAAM,UAAU;CACrD,MAAM,UAAU,OAAO,WAAW,MAAM,UAAU,KAAK;CACvD,MAAM,SAAS,OAAO,SAAS,UAAU,IAAI,KAAK,IAAI,YAAY,KAAK,MAAM,IAAI,KAAK;CACtF,OAAO;EAAE,KAAK,KAAK,OAAO,SAAS,KAAK,SAAS,UAAU;EAAI;CAAO;AACxE"}
|
|
1
|
+
{"version":3,"file":"dom.js","names":[],"sources":["../src/dom.ts"],"sourcesContent":["import type { EditorView } from 'prosemirror-view'\n\n/**\n * The content element (paragraph, heading, …) at height `y` in viewport\n * coordinates: the last one whose top is above it. Contents follow each\n * other from top to bottom in document order, nested ones included, so\n * a binary search does.\n */\nexport function contentAt(view: EditorView, y: number): HTMLElement | null {\n const list = view.dom.querySelectorAll<HTMLElement>('.dou-editor__content')\n let low = 0\n let high = list.length - 1\n let found = -1\n while (low <= high) {\n const middle = (low + high) >> 1\n if (list[middle]!.getBoundingClientRect().top <= y) {\n found = middle\n low = middle + 1\n } else high = middle - 1\n }\n return list[Math.max(found, 0)] ?? null\n}\n\n/** The block element around a content element. */\nexport function blockElementOf(content: HTMLElement): HTMLElement | null {\n return content.closest<HTMLElement>('.dou-editor__block')\n}\n\n/** The nearest ancestor that scrolls vertically, or `null` for the page. */\nexport function scrollParent(element: HTMLElement): HTMLElement | null {\n for (let node = element.parentElement; node; node = node.parentElement) {\n const { overflowY } = getComputedStyle(node)\n if ((overflowY === 'auto' || overflowY === 'scroll') && node.scrollHeight > node.clientHeight)\n return node\n }\n return null\n}\n\n/** Where the first line of a content element sits: its top and height. */\nexport function firstLine(content: HTMLElement): { top: number; height: number } {\n const rect = content.getBoundingClientRect()\n const style = getComputedStyle(content)\n const lineHeight = Number.parseFloat(style.lineHeight)\n const padding = Number.parseFloat(style.paddingTop) || 0\n const height = Number.isFinite(lineHeight) ? Math.min(lineHeight, rect.height) : rect.height\n return { top: rect.top + (height < rect.height ? padding : 0), height }\n}\n\n/** The right edge of the editor's text column: the surface less its padding (a reading width). */\nexport function columnRight(surface: HTMLElement): number {\n const padding = Number.parseFloat(getComputedStyle(surface).paddingRight) || 0\n return surface.getBoundingClientRect().right - padding\n}\n"],"mappings":";;;;;;;AAQA,SAAgB,UAAU,MAAkB,GAA+B;CACzE,MAAM,OAAO,KAAK,IAAI,iBAA8B,sBAAsB;CAC1E,IAAI,MAAM;CACV,IAAI,OAAO,KAAK,SAAS;CACzB,IAAI,QAAQ;CACZ,OAAO,OAAO,MAAM;EAClB,MAAM,SAAU,MAAM,QAAS;EAC/B,IAAI,KAAK,OAAO,CAAE,sBAAsB,CAAC,CAAC,OAAO,GAAG;GAClD,QAAQ;GACR,MAAM,SAAS;EACjB,OAAO,OAAO,SAAS;CACzB;CACA,OAAO,KAAK,KAAK,IAAI,OAAO,CAAC,MAAM;AACrC;;AAGA,SAAgB,eAAe,SAA0C;CACvE,OAAO,QAAQ,QAAqB,oBAAoB;AAC1D;;AAGA,SAAgB,aAAa,SAA0C;CACrE,KAAK,IAAI,OAAO,QAAQ,eAAe,MAAM,OAAO,KAAK,eAAe;EACtE,MAAM,EAAE,cAAc,iBAAiB,IAAI;EAC3C,KAAK,cAAc,UAAU,cAAc,aAAa,KAAK,eAAe,KAAK,cAC/E,OAAO;CACX;CACA,OAAO;AACT;;AAGA,SAAgB,UAAU,SAAuD;CAC/E,MAAM,OAAO,QAAQ,sBAAsB;CAC3C,MAAM,QAAQ,iBAAiB,OAAO;CACtC,MAAM,aAAa,OAAO,WAAW,MAAM,UAAU;CACrD,MAAM,UAAU,OAAO,WAAW,MAAM,UAAU,KAAK;CACvD,MAAM,SAAS,OAAO,SAAS,UAAU,IAAI,KAAK,IAAI,YAAY,KAAK,MAAM,IAAI,KAAK;CACtF,OAAO;EAAE,KAAK,KAAK,OAAO,SAAS,KAAK,SAAS,UAAU;EAAI;CAAO;AACxE;;AAGA,SAAgB,YAAY,SAA8B;CACxD,MAAM,UAAU,OAAO,WAAW,iBAAiB,OAAO,CAAC,CAAC,YAAY,KAAK;CAC7E,OAAO,QAAQ,sBAAsB,CAAC,CAAC,QAAQ;AACjD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"align-menu.js","names":["_sfc_main"],"sources":["../../../src/extensions/align/align-menu.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { TextAlignCenterIcon, TextAlignLeftIcon, TextAlignRightIcon } from '@dou-ui/icons'\nimport { DouButton, DouDropdown, DouDropdownItem, DouDropdownRadioGroup, DouTooltip } from 'dou-ui'\nimport { computed } from 'vue'\nimport type { BlockAlign } from '../../core/align'\nimport { useEditor, useToolbarMenu } from '../../editor'\nimport { currentAlign, setAlign } from './commands'\n\nconst { view, state, messages } = useEditor()\n\nconst icons = { left: TextAlignLeftIcon, center: TextAlignCenterIcon, right: TextAlignRightIcon }\nconst shortcuts = { left: 'mod+shift+l', center: 'mod+shift+e', right: 'mod+shift+r' }\nconst aligns = ['left', 'center', 'right'] as const\n\nconst align = computed(() => (state.value ? currentAlign(state.value) : undefined))\n\nfunction choose(value: unknown) {\n const current = view.value\n if (!current) return\n // left is the reading direction: store nothing\n const next = value === 'left' ? null : (value as BlockAlign)\n setAlign(next)(current.state, current.dispatch)\n current.focus()\n}\n\nconst open = useToolbarMenu()\n</script>\n\n<template>\n <DouTooltip
|
|
1
|
+
{"version":3,"file":"align-menu.js","names":["_sfc_main"],"sources":["../../../src/extensions/align/align-menu.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { TextAlignCenterIcon, TextAlignLeftIcon, TextAlignRightIcon } from '@dou-ui/icons'\nimport { DouButton, DouDropdown, DouDropdownItem, DouDropdownRadioGroup, DouTooltip } from 'dou-ui'\nimport { computed } from 'vue'\nimport type { BlockAlign } from '../../core/align'\nimport { useEditor, useToolbarMenu } from '../../editor'\nimport { currentAlign, setAlign } from './commands'\n\nconst { view, state, messages } = useEditor()\n\nconst icons = { left: TextAlignLeftIcon, center: TextAlignCenterIcon, right: TextAlignRightIcon }\nconst shortcuts = { left: 'mod+shift+l', center: 'mod+shift+e', right: 'mod+shift+r' }\nconst aligns = ['left', 'center', 'right'] as const\n\nconst align = computed(() => (state.value ? currentAlign(state.value) : undefined))\n\nfunction choose(value: unknown) {\n const current = view.value\n if (!current) return\n // left is the reading direction: store nothing\n const next = value === 'left' ? null : (value as BlockAlign)\n setAlign(next)(current.state, current.dispatch)\n current.focus()\n}\n\nconst open = useToolbarMenu()\n</script>\n\n<template>\n <DouTooltip v-if=\"align !== undefined\" :content=\"messages.align.label\" placement=\"bottom\">\n <DouDropdown v-model:open=\"open\" placement=\"bottom-start\" class=\"dou-editor-menu\">\n <DouButton size=\"sm\" variant=\"ghost\" :aria-label=\"messages.align.label\">\n <component :is=\"icons[align === 'mixed' || !align ? 'left' : align]\" />\n </DouButton>\n <template #menu>\n <DouDropdownRadioGroup\n :model-value=\"align === 'mixed' ? undefined : (align ?? 'left')\"\n @update:model-value=\"choose\"\n >\n <DouDropdownItem\n v-for=\"name in aligns\"\n :key=\"name\"\n :value=\"name\"\n :shortcut=\"shortcuts[name]\"\n >\n <template #prefix><component :is=\"icons[name]\" /></template>\n {{ messages.align[name] }}\n </DouDropdownItem>\n </DouDropdownRadioGroup>\n </template>\n </DouDropdown>\n </DouTooltip>\n</template>\n"],"mappings":""}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { useEditor, useToolbarMenu } from "../../editor.js";
|
|
2
2
|
import { currentAlign, setAlign } from "./commands.js";
|
|
3
3
|
import { DouButton, DouDropdown, DouDropdownItem, DouDropdownRadioGroup, DouTooltip } from "dou-ui";
|
|
4
|
-
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock,
|
|
4
|
+
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createTextVNode, createVNode, defineComponent, isRef, openBlock, renderList, resolveDynamicComponent, toDisplayString, unref, withCtx } from "vue";
|
|
5
5
|
import { TextAlignCenterIcon, TextAlignLeftIcon, TextAlignRightIcon } from "@dou-ui/icons";
|
|
6
6
|
// src/extensions/align/align-menu.vue?vue&type=script&setup=true&lang.ts
|
|
7
|
-
var _hoisted_1 = { class: "dou-editor-bubble__trigger" };
|
|
8
7
|
var align_menu_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defineComponent({
|
|
9
8
|
__name: "align-menu",
|
|
10
9
|
setup(__props) {
|
|
@@ -36,10 +35,9 @@ var align_menu_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defin
|
|
|
36
35
|
return align.value !== void 0 ? (openBlock(), createBlock(unref(DouTooltip), {
|
|
37
36
|
key: 0,
|
|
38
37
|
content: unref(messages).align.label,
|
|
39
|
-
disabled: unref(open),
|
|
40
38
|
placement: "bottom"
|
|
41
39
|
}, {
|
|
42
|
-
default: withCtx(() => [
|
|
40
|
+
default: withCtx(() => [createVNode(unref(DouDropdown), {
|
|
43
41
|
open: unref(open),
|
|
44
42
|
"onUpdate:open": _cache[0] || (_cache[0] = ($event) => isRef(open) ? open.value = $event : null),
|
|
45
43
|
placement: "bottom-start",
|
|
@@ -71,9 +69,9 @@ var align_menu_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defin
|
|
|
71
69
|
_: 1
|
|
72
70
|
}, 8, ["aria-label"])]),
|
|
73
71
|
_: 1
|
|
74
|
-
}, 8, ["open"])])
|
|
72
|
+
}, 8, ["open"])]),
|
|
75
73
|
_: 1
|
|
76
|
-
}, 8, ["content"
|
|
74
|
+
}, 8, ["content"])) : createCommentVNode("", true);
|
|
77
75
|
};
|
|
78
76
|
}
|
|
79
77
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"align-menu.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createBlock","_unref","_withCtx","
|
|
1
|
+
{"version":3,"file":"align-menu.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createBlock","_unref","_withCtx","_createVNode","_isRef","_createElementBlock","_Fragment","_renderList","_resolveDynamicComponent","_createTextVNode","_toDisplayString","_createCommentVNode"],"sources":["../../../src/extensions/align/align-menu.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { TextAlignCenterIcon, TextAlignLeftIcon, TextAlignRightIcon } from '@dou-ui/icons'\nimport { DouButton, DouDropdown, DouDropdownItem, DouDropdownRadioGroup, DouTooltip } from 'dou-ui'\nimport { computed } from 'vue'\nimport type { BlockAlign } from '../../core/align'\nimport { useEditor, useToolbarMenu } from '../../editor'\nimport { currentAlign, setAlign } from './commands'\n\nconst { view, state, messages } = useEditor()\n\nconst icons = { left: TextAlignLeftIcon, center: TextAlignCenterIcon, right: TextAlignRightIcon }\nconst shortcuts = { left: 'mod+shift+l', center: 'mod+shift+e', right: 'mod+shift+r' }\nconst aligns = ['left', 'center', 'right'] as const\n\nconst align = computed(() => (state.value ? currentAlign(state.value) : undefined))\n\nfunction choose(value: unknown) {\n const current = view.value\n if (!current) return\n // left is the reading direction: store nothing\n const next = value === 'left' ? null : (value as BlockAlign)\n setAlign(next)(current.state, current.dispatch)\n current.focus()\n}\n\nconst open = useToolbarMenu()\n</script>\n\n<template>\n <DouTooltip v-if=\"align !== undefined\" :content=\"messages.align.label\" placement=\"bottom\">\n <DouDropdown v-model:open=\"open\" placement=\"bottom-start\" class=\"dou-editor-menu\">\n <DouButton size=\"sm\" variant=\"ghost\" :aria-label=\"messages.align.label\">\n <component :is=\"icons[align === 'mixed' || !align ? 'left' : align]\" />\n </DouButton>\n <template #menu>\n <DouDropdownRadioGroup\n :model-value=\"align === 'mixed' ? undefined : (align ?? 'left')\"\n @update:model-value=\"choose\"\n >\n <DouDropdownItem\n v-for=\"name in aligns\"\n :key=\"name\"\n :value=\"name\"\n :shortcut=\"shortcuts[name]\"\n >\n <template #prefix><component :is=\"icons[name]\" /></template>\n {{ messages.align[name] }}\n </DouDropdownItem>\n </DouDropdownRadioGroup>\n </template>\n </DouDropdown>\n </DouTooltip>\n</template>\n"],"mappings":";;;;;;;;;EAQA,MAAM,EAAE,MAAM,OAAO,aAAa,UAAU;EAE5C,MAAM,QAAQ;GAAE,MAAM;GAAmB,QAAQ;GAAqB,OAAO;EAAmB;EAChG,MAAM,YAAY;GAAE,MAAM;GAAe,QAAQ;GAAe,OAAO;EAAc;EACrF,MAAM,SAAS;GAAC;GAAQ;GAAU;EAAO;EAEzC,MAAM,QAAQ,eAAgB,MAAM,QAAQ,aAAa,MAAM,KAAK,IAAI,KAAA,CAAU;EAElF,SAAS,OAAO,OAAgB;GAC9B,MAAM,UAAU,KAAK;GACrB,IAAI,CAAC,SAAS;GAGd,SADa,UAAU,SAAS,OAAQ,KAC3B,CAAC,CAAC,QAAQ,OAAO,QAAQ,QAAQ;GAC9C,QAAQ,MAAM;EAChB;EAEA,MAAM,OAAO,eAAe;;GAIR,OAAA,MAAA,UAAU,KAAA,KAA5BA,UAAA,GAAAC,YAsBaC,MAAA,UAAA,GAAA;;IAtB2B,SAASA,MAAA,QAAA,CAAQ,CAAC,MAAM;IAAO,WAAU;;IAC/E,SAAAC,cAoBc,CApBdC,YAoBcF,MAAA,WAAA,GAAA;KApBO,MAAMA,MAAA,IAAA;KAAA,iBAAA,OAAA,OAAA,OAAA,MAAA,WAAAG,MAAA,IAAA,IAAA,KAAI,QAAA,SAAA;KAAE,WAAU;KAAe,OAAM;;KAInD,MAAIF,cAcW,CAbxBC,YAawBF,MAAA,qBAAA,GAAA;MAZrB,eAAa,MAAA,UAAK,UAAe,KAAA,IAAa,MAAA,SAAK;MACnD,uBAAoB;;MAGnB,SAAAC,cAAsB,EADxBH,UAAA,GAAAM,mBAQkBC,UAAA,MAAAC,WAPD,SAAR,SAAI;OADb,OAAAJ,YAQkBF,MAAA,eAAA,GAAA;QANf,KAAK;QACL,OAAO;QACP,UAAU,UAAU;;QAEV,QAAMC,cAAgC,EAA/BH,UAAA,GAAAC,YAA+BQ,wBAAf,MAAM,KAAI,CAAA,E;QAAgB,SAAAN,cAC5D,CAD4DO,gBAAA,MAC5DC,gBAAGT,MAAA,QAAA,CAAQ,CAAC,MAAM,KAAI,GAAA,CAAA,C;;;;;;KAf5B,SAAAC,cAEY,CAFZC,YAEYF,MAAA,SAAA,GAAA;MAFD,MAAK;MAAK,SAAQ;MAAS,cAAYA,MAAA,QAAA,CAAQ,CAAC,MAAM;;MAC/D,SAAAC,cAAuE,EAAvEH,UAAA,GAAAC,YAAuEQ,wBAAvD,MAAM,MAAA,UAAK,WAAA,CAAiB,MAAA,QAAK,SAAY,MAAA,MAAK,CAAA,E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"color-menu.js","names":["_sfc_main"],"sources":["../../../src/extensions/colors/color-menu.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { CheckIcon } from '@dou-ui/icons'\nimport { DouButton, DouDropdown, DouDropdownItem, DouDropdownSub, DouTooltip } from 'dou-ui'\nimport { computed } from 'vue'\nimport { useEditor, useToolbarMenu } from '../../editor'\nimport { setHighlight, setTextColor, textColors, type TextColor } from './palette'\n\nconst { view, state, messages } = useEditor()\n\n/** The color the whole selection has, `null` for none, `'mixed'` for several. */\nfunction current(name: string): string | null {\n const now = state.value\n const type = now?.schema.marks[name]\n if (!now || !type) return null\n const { from, to } = now.selection\n let value: string | null | undefined\n now.doc.nodesBetween(from, to, (node) => {\n if (!node.isText || value === 'mixed') return\n const found = (type.isInSet(node.marks)?.attrs.value as string | undefined) ?? null\n value = value === undefined || value === found ? found : 'mixed'\n })\n return value ?? null\n}\n\n/** The swatch shows one color; several show as the default. */\nconst shown = (value: string | null) => (value && value !== 'mixed' ? value : undefined)\n\nconst color = computed(() => current('textColor'))\nconst highlight = computed(() => current('highlight'))\n\nfunction apply(kind: 'text' | 'background', value: TextColor | null) {\n const target = view.value\n if (!target) return\n ;(kind === 'text' ? setTextColor : setHighlight)(value)(target.state, target.dispatch)\n target.focus()\n}\n\nconst open = useToolbarMenu()\n</script>\n\n<template>\n <DouTooltip :content=\"messages.colors.label\"
|
|
1
|
+
{"version":3,"file":"color-menu.js","names":["_sfc_main"],"sources":["../../../src/extensions/colors/color-menu.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { CheckIcon } from '@dou-ui/icons'\nimport { DouButton, DouDropdown, DouDropdownItem, DouDropdownSub, DouTooltip } from 'dou-ui'\nimport { computed } from 'vue'\nimport { useEditor, useToolbarMenu } from '../../editor'\nimport { setHighlight, setTextColor, textColors, type TextColor } from './palette'\n\nconst { view, state, messages } = useEditor()\n\n/** The color the whole selection has, `null` for none, `'mixed'` for several. */\nfunction current(name: string): string | null {\n const now = state.value\n const type = now?.schema.marks[name]\n if (!now || !type) return null\n const { from, to } = now.selection\n let value: string | null | undefined\n now.doc.nodesBetween(from, to, (node) => {\n if (!node.isText || value === 'mixed') return\n const found = (type.isInSet(node.marks)?.attrs.value as string | undefined) ?? null\n value = value === undefined || value === found ? found : 'mixed'\n })\n return value ?? null\n}\n\n/** The swatch shows one color; several show as the default. */\nconst shown = (value: string | null) => (value && value !== 'mixed' ? value : undefined)\n\nconst color = computed(() => current('textColor'))\nconst highlight = computed(() => current('highlight'))\n\nfunction apply(kind: 'text' | 'background', value: TextColor | null) {\n const target = view.value\n if (!target) return\n ;(kind === 'text' ? setTextColor : setHighlight)(value)(target.state, target.dispatch)\n target.focus()\n}\n\nconst open = useToolbarMenu()\n</script>\n\n<template>\n <DouTooltip :content=\"messages.colors.label\" placement=\"bottom\">\n <DouDropdown v-model:open=\"open\" placement=\"bottom-start\" class=\"dou-editor-menu\">\n <DouButton\n size=\"sm\"\n variant=\"ghost\"\n class=\"dou-editor-colors__button\"\n :aria-label=\"messages.colors.label\"\n >\n <template #leading>\n <span\n class=\"dou-editor-colors__swatch\"\n :data-color=\"shown(color)\"\n :data-highlight=\"shown(highlight)\"\n aria-hidden=\"true\"\n ><span class=\"dou-editor-colors__letter\">A</span></span\n >\n </template>\n </DouButton>\n <template #menu>\n <DouDropdownSub\n v-for=\"kind in ['text', 'background'] as const\"\n :key=\"kind\"\n :title=\"kind === 'text' ? messages.colors.text : messages.colors.background\"\n >\n <DouDropdownItem @click=\"apply(kind, null)\">\n <template #prefix>\n <span class=\"dou-editor-colors__swatch\" aria-hidden=\"true\"\n ><span class=\"dou-editor-colors__letter\">A</span></span\n >\n </template>\n {{ messages.colors.none }}\n <template v-if=\"!(kind === 'text' ? color : highlight)\" #suffix><CheckIcon /></template>\n </DouDropdownItem>\n <DouDropdownItem v-for=\"name in textColors\" :key=\"name\" @click=\"apply(kind, name)\">\n <template #prefix>\n <span\n class=\"dou-editor-colors__swatch\"\n :data-color=\"kind === 'text' ? name : undefined\"\n :data-highlight=\"kind === 'background' ? name : undefined\"\n aria-hidden=\"true\"\n ><span class=\"dou-editor-colors__letter\">A</span></span\n >\n </template>\n {{ messages.colors.names[name] }}\n <template v-if=\"(kind === 'text' ? color : highlight) === name\" #suffix>\n <CheckIcon />\n </template>\n </DouDropdownItem>\n </DouDropdownSub>\n </template>\n </DouDropdown>\n </DouTooltip>\n</template>\n"],"mappings":""}
|
|
@@ -5,9 +5,8 @@ import { DouButton, DouDropdown, DouDropdownItem, DouDropdownSub, DouTooltip } f
|
|
|
5
5
|
import { Fragment, computed, createBlock, createElementBlock, createElementVNode, createSlots, createTextVNode, createVNode, defineComponent, isRef, openBlock, renderList, toDisplayString, unref, withCtx } from "vue";
|
|
6
6
|
import { CheckIcon } from "@dou-ui/icons";
|
|
7
7
|
// src/extensions/colors/color-menu.vue?vue&type=script&setup=true&lang.ts
|
|
8
|
-
var _hoisted_1 =
|
|
8
|
+
var _hoisted_1 = ["data-color", "data-highlight"];
|
|
9
9
|
var _hoisted_2 = ["data-color", "data-highlight"];
|
|
10
|
-
var _hoisted_3 = ["data-color", "data-highlight"];
|
|
11
10
|
var color_menu_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defineComponent({
|
|
12
11
|
__name: "color-menu",
|
|
13
12
|
setup(__props) {
|
|
@@ -40,10 +39,9 @@ var color_menu_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defin
|
|
|
40
39
|
return (_ctx, _cache) => {
|
|
41
40
|
return openBlock(), createBlock(unref(DouTooltip), {
|
|
42
41
|
content: unref(messages).colors.label,
|
|
43
|
-
disabled: unref(open),
|
|
44
42
|
placement: "bottom"
|
|
45
43
|
}, {
|
|
46
|
-
default: withCtx(() => [
|
|
44
|
+
default: withCtx(() => [createVNode(unref(DouDropdown), {
|
|
47
45
|
open: unref(open),
|
|
48
46
|
"onUpdate:open": _cache[0] || (_cache[0] = ($event) => isRef(open) ? open.value = $event : null),
|
|
49
47
|
placement: "bottom-start",
|
|
@@ -75,7 +73,7 @@ var color_menu_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defin
|
|
|
75
73
|
"data-color": kind === "text" ? name : void 0,
|
|
76
74
|
"data-highlight": kind === "background" ? name : void 0,
|
|
77
75
|
"aria-hidden": "true"
|
|
78
|
-
}, [..._cache[3] || (_cache[3] = [createElementVNode("span", { class: "dou-editor-colors__letter" }, "A", -1)])], 8,
|
|
76
|
+
}, [..._cache[3] || (_cache[3] = [createElementVNode("span", { class: "dou-editor-colors__letter" }, "A", -1)])], 8, _hoisted_2)]),
|
|
79
77
|
default: withCtx(() => [createTextVNode(" " + toDisplayString(unref(messages).colors.names[name]) + " ", 1)]),
|
|
80
78
|
_: 2
|
|
81
79
|
}, [(kind === "text" ? color.value : highlight.value) === name ? {
|
|
@@ -98,13 +96,13 @@ var color_menu_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defin
|
|
|
98
96
|
"data-color": shown(color.value),
|
|
99
97
|
"data-highlight": shown(highlight.value),
|
|
100
98
|
"aria-hidden": "true"
|
|
101
|
-
}, [..._cache[1] || (_cache[1] = [createElementVNode("span", { class: "dou-editor-colors__letter" }, "A", -1)])], 8,
|
|
99
|
+
}, [..._cache[1] || (_cache[1] = [createElementVNode("span", { class: "dou-editor-colors__letter" }, "A", -1)])], 8, _hoisted_1)]),
|
|
102
100
|
_: 1
|
|
103
101
|
}, 8, ["aria-label"])]),
|
|
104
102
|
_: 1
|
|
105
|
-
}, 8, ["open"])])
|
|
103
|
+
}, 8, ["open"])]),
|
|
106
104
|
_: 1
|
|
107
|
-
}, 8, ["content"
|
|
105
|
+
}, 8, ["content"]);
|
|
108
106
|
};
|
|
109
107
|
}
|
|
110
108
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"color-menu.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createBlock","_unref","_withCtx","
|
|
1
|
+
{"version":3,"file":"color-menu.vue_vue_type_script_setup_true_lang.js","names":["_openBlock","_createBlock","_unref","_withCtx","_createVNode","_isRef","_createElementBlock","_Fragment","_renderList","_createSlots","_createElementVNode","_createTextVNode","_toDisplayString"],"sources":["../../../src/extensions/colors/color-menu.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { CheckIcon } from '@dou-ui/icons'\nimport { DouButton, DouDropdown, DouDropdownItem, DouDropdownSub, DouTooltip } from 'dou-ui'\nimport { computed } from 'vue'\nimport { useEditor, useToolbarMenu } from '../../editor'\nimport { setHighlight, setTextColor, textColors, type TextColor } from './palette'\n\nconst { view, state, messages } = useEditor()\n\n/** The color the whole selection has, `null` for none, `'mixed'` for several. */\nfunction current(name: string): string | null {\n const now = state.value\n const type = now?.schema.marks[name]\n if (!now || !type) return null\n const { from, to } = now.selection\n let value: string | null | undefined\n now.doc.nodesBetween(from, to, (node) => {\n if (!node.isText || value === 'mixed') return\n const found = (type.isInSet(node.marks)?.attrs.value as string | undefined) ?? null\n value = value === undefined || value === found ? found : 'mixed'\n })\n return value ?? null\n}\n\n/** The swatch shows one color; several show as the default. */\nconst shown = (value: string | null) => (value && value !== 'mixed' ? value : undefined)\n\nconst color = computed(() => current('textColor'))\nconst highlight = computed(() => current('highlight'))\n\nfunction apply(kind: 'text' | 'background', value: TextColor | null) {\n const target = view.value\n if (!target) return\n ;(kind === 'text' ? setTextColor : setHighlight)(value)(target.state, target.dispatch)\n target.focus()\n}\n\nconst open = useToolbarMenu()\n</script>\n\n<template>\n <DouTooltip :content=\"messages.colors.label\" placement=\"bottom\">\n <DouDropdown v-model:open=\"open\" placement=\"bottom-start\" class=\"dou-editor-menu\">\n <DouButton\n size=\"sm\"\n variant=\"ghost\"\n class=\"dou-editor-colors__button\"\n :aria-label=\"messages.colors.label\"\n >\n <template #leading>\n <span\n class=\"dou-editor-colors__swatch\"\n :data-color=\"shown(color)\"\n :data-highlight=\"shown(highlight)\"\n aria-hidden=\"true\"\n ><span class=\"dou-editor-colors__letter\">A</span></span\n >\n </template>\n </DouButton>\n <template #menu>\n <DouDropdownSub\n v-for=\"kind in ['text', 'background'] as const\"\n :key=\"kind\"\n :title=\"kind === 'text' ? messages.colors.text : messages.colors.background\"\n >\n <DouDropdownItem @click=\"apply(kind, null)\">\n <template #prefix>\n <span class=\"dou-editor-colors__swatch\" aria-hidden=\"true\"\n ><span class=\"dou-editor-colors__letter\">A</span></span\n >\n </template>\n {{ messages.colors.none }}\n <template v-if=\"!(kind === 'text' ? color : highlight)\" #suffix><CheckIcon /></template>\n </DouDropdownItem>\n <DouDropdownItem v-for=\"name in textColors\" :key=\"name\" @click=\"apply(kind, name)\">\n <template #prefix>\n <span\n class=\"dou-editor-colors__swatch\"\n :data-color=\"kind === 'text' ? name : undefined\"\n :data-highlight=\"kind === 'background' ? name : undefined\"\n aria-hidden=\"true\"\n ><span class=\"dou-editor-colors__letter\">A</span></span\n >\n </template>\n {{ messages.colors.names[name] }}\n <template v-if=\"(kind === 'text' ? color : highlight) === name\" #suffix>\n <CheckIcon />\n </template>\n </DouDropdownItem>\n </DouDropdownSub>\n </template>\n </DouDropdown>\n </DouTooltip>\n</template>\n"],"mappings":";;;;;;;;;;;;EAOA,MAAM,EAAE,MAAM,OAAO,aAAa,UAAU;;EAG5C,SAAS,QAAQ,MAA6B;GAC5C,MAAM,MAAM,MAAM;GAClB,MAAM,OAAO,KAAK,OAAO,MAAM;GAC/B,IAAI,CAAC,OAAO,CAAC,MAAM,OAAO;GAC1B,MAAM,EAAE,MAAM,OAAO,IAAI;GACzB,IAAI;GACJ,IAAI,IAAI,aAAa,MAAM,KAAK,SAAS;IACvC,IAAI,CAAC,KAAK,UAAU,UAAU,SAAS;IACvC,MAAM,QAAS,KAAK,QAAQ,KAAK,KAAK,CAAC,EAAE,MAAM,SAAgC;IAC/E,QAAQ,UAAU,KAAA,KAAa,UAAU,QAAQ,QAAQ;GAC3D,CAAC;GACD,OAAO,SAAS;EAClB;;EAGA,MAAM,SAAS,UAA0B,SAAS,UAAU,UAAU,QAAQ,KAAA;EAE9E,MAAM,QAAQ,eAAe,QAAQ,WAAW,CAAC;EACjD,MAAM,YAAY,eAAe,QAAQ,WAAW,CAAC;EAErD,SAAS,MAAM,MAA6B,OAAyB;GACnE,MAAM,SAAS,KAAK;GACpB,IAAI,CAAC,QAAQ;GACZ,CAAC,SA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