@salmonui/rich-text 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +79 -2
- package/dist/blocks/find.d.ts +4 -0
- package/dist/blocks/find.d.ts.map +1 -0
- package/dist/blocks/find.js +14 -0
- package/dist/blocks/find.js.map +1 -0
- package/dist/blocks/from-html.d.ts +10 -0
- package/dist/blocks/from-html.d.ts.map +1 -0
- package/dist/blocks/from-html.js +459 -0
- package/dist/blocks/from-html.js.map +1 -0
- package/dist/blocks/index.d.ts +5 -0
- package/dist/blocks/index.d.ts.map +1 -0
- package/dist/blocks/index.js +4 -0
- package/dist/blocks/index.js.map +1 -0
- package/dist/blocks/ops.d.ts +27 -0
- package/dist/blocks/ops.d.ts.map +1 -0
- package/dist/blocks/ops.js +172 -0
- package/dist/blocks/ops.js.map +1 -0
- package/dist/blocks/to-html.d.ts +18 -0
- package/dist/blocks/to-html.d.ts.map +1 -0
- package/dist/blocks/to-html.js +207 -0
- package/dist/blocks/to-html.js.map +1 -0
- package/dist/blocks/types.d.ts +76 -0
- package/dist/blocks/types.d.ts.map +1 -0
- package/dist/blocks/types.js +4 -0
- package/dist/blocks/types.js.map +1 -0
- package/dist/collab.d.ts +43 -0
- package/dist/collab.d.ts.map +1 -0
- package/dist/collab.js +45 -0
- package/dist/collab.js.map +1 -0
- package/dist/content/RichTextContent.d.ts +19 -0
- package/dist/content/RichTextContent.d.ts.map +1 -0
- package/dist/content/RichTextContent.js +20 -0
- package/dist/content/RichTextContent.js.map +1 -0
- package/dist/content/index.d.ts +2 -0
- package/dist/content/index.d.ts.map +1 -0
- package/dist/content/index.js +3 -0
- package/dist/content/index.js.map +1 -0
- package/dist/engine/commands.d.ts +19 -0
- package/dist/engine/commands.d.ts.map +1 -0
- package/dist/engine/commands.js +457 -0
- package/dist/engine/commands.js.map +1 -0
- package/dist/engine/core.d.ts +116 -0
- package/dist/engine/core.d.ts.map +1 -0
- package/dist/engine/core.js +460 -0
- package/dist/engine/core.js.map +1 -0
- package/dist/engine/dom-shim.d.ts +47 -0
- package/dist/engine/dom-shim.d.ts.map +1 -0
- package/dist/engine/dom-shim.js +128 -0
- package/dist/engine/dom-shim.js.map +1 -0
- package/dist/engine/html-out.d.ts +12 -0
- package/dist/engine/html-out.d.ts.map +1 -0
- package/dist/engine/html-out.js +24 -0
- package/dist/engine/html-out.js.map +1 -0
- package/dist/engine/index.d.ts +6 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +5 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/keymap.d.ts +18 -0
- package/dist/engine/keymap.d.ts.map +1 -0
- package/dist/engine/keymap.js +85 -0
- package/dist/engine/keymap.js.map +1 -0
- package/dist/engine/load.d.ts +10 -0
- package/dist/engine/load.d.ts.map +1 -0
- package/dist/engine/load.js +11 -0
- package/dist/engine/load.js.map +1 -0
- package/dist/engine/nodeviews.d.ts +19 -0
- package/dist/engine/nodeviews.d.ts.map +1 -0
- package/dist/engine/nodeviews.js +241 -0
- package/dist/engine/nodeviews.js.map +1 -0
- package/dist/engine/plugins.d.ts +77 -0
- package/dist/engine/plugins.d.ts.map +1 -0
- package/dist/engine/plugins.js +312 -0
- package/dist/engine/plugins.js.map +1 -0
- package/dist/engine/schema.d.ts +11 -0
- package/dist/engine/schema.d.ts.map +1 -0
- package/dist/engine/schema.js +350 -0
- package/dist/engine/schema.js.map +1 -0
- package/dist/engine/serialize.d.ts +13 -0
- package/dist/engine/serialize.d.ts.map +1 -0
- package/dist/engine/serialize.js +119 -0
- package/dist/engine/serialize.js.map +1 -0
- package/dist/engine/snapshot.d.ts +4 -0
- package/dist/engine/snapshot.d.ts.map +1 -0
- package/dist/engine/snapshot.js +62 -0
- package/dist/engine/snapshot.js.map +1 -0
- package/dist/engine/types.d.ts +215 -0
- package/dist/engine/types.d.ts.map +1 -0
- package/dist/engine/types.js +2 -0
- package/dist/engine/types.js.map +1 -0
- package/dist/html/entities.d.ts +7 -0
- package/dist/html/entities.d.ts.map +1 -0
- package/dist/html/entities.js +69 -0
- package/dist/html/entities.js.map +1 -0
- package/dist/html/index.d.ts +7 -0
- package/dist/html/index.d.ts.map +1 -0
- package/dist/html/index.js +9 -0
- package/dist/html/index.js.map +1 -0
- package/dist/html/parse.d.ts +30 -0
- package/dist/html/parse.d.ts.map +1 -0
- package/dist/html/parse.js +394 -0
- package/dist/html/parse.js.map +1 -0
- package/dist/html/paste.d.ts +47 -0
- package/dist/html/paste.d.ts.map +1 -0
- package/dist/html/paste.js +373 -0
- package/dist/html/paste.js.map +1 -0
- package/dist/html/policy.d.ts +56 -0
- package/dist/html/policy.d.ts.map +1 -0
- package/dist/html/policy.js +245 -0
- package/dist/html/policy.js.map +1 -0
- package/dist/html/sanitize.d.ts +60 -0
- package/dist/html/sanitize.d.ts.map +1 -0
- package/dist/html/sanitize.js +699 -0
- package/dist/html/sanitize.js.map +1 -0
- package/dist/html/serialize.d.ts +16 -0
- package/dist/html/serialize.d.ts.map +1 -0
- package/dist/html/serialize.js +68 -0
- package/dist/html/serialize.js.map +1 -0
- package/dist/html/text.d.ts +18 -0
- package/dist/html/text.d.ts.map +1 -0
- package/dist/html/text.js +96 -0
- package/dist/html/text.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/mention/editable.d.ts +15 -0
- package/dist/mention/editable.d.ts.map +1 -0
- package/dist/mention/editable.js +167 -0
- package/dist/mention/editable.js.map +1 -0
- package/dist/mention/index.d.ts +2 -0
- package/dist/mention/index.d.ts.map +1 -0
- package/dist/mention/index.js +3 -0
- package/dist/mention/index.js.map +1 -0
- package/dist/react/RichTextEditor.d.ts +15 -0
- package/dist/react/RichTextEditor.d.ts.map +1 -0
- package/dist/react/RichTextEditor.js +497 -0
- package/dist/react/RichTextEditor.js.map +1 -0
- package/dist/react/block-editor/BlockEditor.d.ts +5 -0
- package/dist/react/block-editor/BlockEditor.d.ts.map +1 -0
- package/dist/react/block-editor/BlockEditor.js +106 -0
- package/dist/react/block-editor/BlockEditor.js.map +1 -0
- package/dist/react/block-editor/controller.d.ts +94 -0
- package/dist/react/block-editor/controller.d.ts.map +1 -0
- package/dist/react/block-editor/controller.js +758 -0
- package/dist/react/block-editor/controller.js.map +1 -0
- package/dist/react/block-editor/index.d.ts +4 -0
- package/dist/react/block-editor/index.d.ts.map +1 -0
- package/dist/react/block-editor/index.js +5 -0
- package/dist/react/block-editor/index.js.map +1 -0
- package/dist/react/block-editor/locale.d.ts +73 -0
- package/dist/react/block-editor/locale.d.ts.map +1 -0
- package/dist/react/block-editor/locale.js +65 -0
- package/dist/react/block-editor/locale.js.map +1 -0
- package/dist/react/block-editor/transforms.d.ts +26 -0
- package/dist/react/block-editor/transforms.d.ts.map +1 -0
- package/dist/react/block-editor/transforms.js +233 -0
- package/dist/react/block-editor/transforms.js.map +1 -0
- package/dist/react/block-editor/types.d.ts +180 -0
- package/dist/react/block-editor/types.d.ts.map +1 -0
- package/dist/react/block-editor/types.js +2 -0
- package/dist/react/block-editor/types.js.map +1 -0
- package/dist/react/block-editor/ui.d.ts +21 -0
- package/dist/react/block-editor/ui.d.ts.map +1 -0
- package/dist/react/block-editor/ui.js +601 -0
- package/dist/react/block-editor/ui.js.map +1 -0
- package/dist/react/client.d.ts +28 -0
- package/dist/react/client.d.ts.map +1 -0
- package/dist/react/client.js +111 -0
- package/dist/react/client.js.map +1 -0
- package/dist/react/dialogs.d.ts +46 -0
- package/dist/react/dialogs.d.ts.map +1 -0
- package/dist/react/dialogs.js +312 -0
- package/dist/react/dialogs.js.map +1 -0
- package/dist/react/emoji-data.d.ts +3 -0
- package/dist/react/emoji-data.d.ts.map +1 -0
- package/dist/react/emoji-data.js +6 -0
- package/dist/react/emoji-data.js.map +1 -0
- package/dist/react/icons.d.ts +5 -0
- package/dist/react/icons.d.ts.map +1 -0
- package/dist/react/icons.js +69 -0
- package/dist/react/icons.js.map +1 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +5 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/items.d.ts +60 -0
- package/dist/react/items.d.ts.map +1 -0
- package/dist/react/items.js +255 -0
- package/dist/react/items.js.map +1 -0
- package/dist/react/locale.d.ts +151 -0
- package/dist/react/locale.d.ts.map +1 -0
- package/dist/react/locale.js +139 -0
- package/dist/react/locale.js.map +1 -0
- package/dist/react/overlay.d.ts +41 -0
- package/dist/react/overlay.d.ts.map +1 -0
- package/dist/react/overlay.js +162 -0
- package/dist/react/overlay.js.map +1 -0
- package/dist/react/store.d.ts +23 -0
- package/dist/react/store.d.ts.map +1 -0
- package/dist/react/store.js +84 -0
- package/dist/react/store.js.map +1 -0
- package/dist/react/types.d.ts +205 -0
- package/dist/react/types.d.ts.map +1 -0
- package/dist/react/types.js +2 -0
- package/dist/react/types.js.map +1 -0
- package/dist/rich-text.css +1555 -0
- package/package.json +61 -4
package/dist/collab.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { redoCommand, undoCommand, yCursorPlugin, ySyncPlugin, yUndoPlugin, prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode } from 'y-prosemirror';
|
|
2
|
+
/**
|
|
3
|
+
* The collaboration extension: pass it in `extensions` (keep it
|
|
4
|
+
* stable, for example in `useMemo` per doc). It binds the editor to the Y.Doc
|
|
5
|
+
* (`ySyncPlugin`), shows remote cursors (`yCursorPlugin`, when `awareness` is
|
|
6
|
+
* given) and replaces the engine history with the per-user Yjs undo manager
|
|
7
|
+
* (`yUndoPlugin`): undo and redo, their keys and the toolbar buttons revert
|
|
8
|
+
* only the local user's changes. The Y.Doc is the source of truth, so do not
|
|
9
|
+
* drive a collaborative editor with a controlled `value`.
|
|
10
|
+
*/
|
|
11
|
+
export function createCollaboration(options) {
|
|
12
|
+
const { doc, awareness, user } = options;
|
|
13
|
+
const type = doc.getXmlFragment(options.fragment ?? 'prosemirror');
|
|
14
|
+
if (awareness)
|
|
15
|
+
awareness.setLocalStateField('user', { id: user.id, name: user.name, color: user.color });
|
|
16
|
+
return {
|
|
17
|
+
name: 'salmonCollaboration',
|
|
18
|
+
replacesHistory: true,
|
|
19
|
+
history: { undo: undoCommand, redo: redoCommand },
|
|
20
|
+
plugins: () => [
|
|
21
|
+
ySyncPlugin(type),
|
|
22
|
+
...(awareness ? [yCursorPlugin(awareness)] : []),
|
|
23
|
+
yUndoPlugin(),
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Seeds an empty Y.Doc with HTML (sanitized and parsed through the shared
|
|
29
|
+
* schema), on the server or by the first client. Seed a doc only once: an
|
|
30
|
+
* editor bound to a seeded doc renders the doc, not its own `value`. Loads
|
|
31
|
+
* the engine on demand, so the `/collab` entry stays small.
|
|
32
|
+
*/
|
|
33
|
+
export async function seedCollaborationDoc(doc, html, options = {}) {
|
|
34
|
+
const { createEngineSync } = await import('./engine/load.js');
|
|
35
|
+
const engine = createEngineSync({ html, ...(options.embedHosts ? { embedHosts: options.embedHosts } : {}) });
|
|
36
|
+
prosemirrorToYXmlFragment(engine.state.doc, doc.getXmlFragment(options.fragment ?? 'prosemirror'));
|
|
37
|
+
}
|
|
38
|
+
/** Sanitized HTML of a Y.Doc (server rendering, persistence, previews with RichTextContent). */
|
|
39
|
+
export async function collaborationDocToHtml(doc, options = {}) {
|
|
40
|
+
const [{ engineSchema, serializeDoc }, { sanitizeHtml }] = await Promise.all([import('./engine/load.js'), import('./html/sanitize.js')]);
|
|
41
|
+
const schema = engineSchema(options.embedHosts ? { embedHosts: options.embedHosts } : {});
|
|
42
|
+
const node = yXmlFragmentToProseMirrorRootNode(doc.getXmlFragment(options.fragment ?? 'prosemirror'), schema);
|
|
43
|
+
return sanitizeHtml(serializeDoc(node), options.embedHosts ? { embedHosts: options.embedHosts } : {});
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=collab.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collab.js","sourceRoot":"","sources":["../src/collab.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,yBAAyB,EAAE,iCAAiC,EAAE,MAAM,eAAe,CAAC;AAqBhK;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAA6B;IAC/D,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;IACzC,MAAM,IAAI,GAAG,GAAG,CAAC,cAAc,CAAC,OAAO,CAAC,QAAQ,IAAI,aAAa,CAAC,CAAC;IACnE,IAAI,SAAS;QAAE,SAAS,CAAC,kBAAkB,CAAC,MAAM,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;IACzG,OAAO;QACL,IAAI,EAAE,qBAAqB;QAC3B,eAAe,EAAE,IAAI;QACrB,OAAO,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,WAAW,EAAE;QACjD,OAAO,EAAE,GAAG,EAAE,CAAC;YACb,WAAW,CAAC,IAAI,CAAC;YACjB,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAChD,WAAW,EAAE;SACd;KACF,CAAC;AACJ,CAAC;AAOD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,GAAU,EAAE,IAAY,EAAE,OAAO,GAA6B,EAAE;IACzG,MAAM,EAAE,gBAAgB,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;IAC9D,MAAM,MAAM,GAAG,gBAAgB,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC7G,yBAAyB,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,cAAc,CAAC,OAAO,CAAC,QAAQ,IAAI,aAAa,CAAC,CAAC,CAAC;AACrG,CAAC;AAED,gGAAgG;AAChG,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,GAAU,EAAE,OAAO,GAA6B,EAAE;IAC7F,MAAM,CAAC,EAAE,YAAY,EAAE,YAAY,EAAE,EAAE,EAAE,YAAY,EAAE,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAAC,EAAE,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC;IACzI,MAAM,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAC1F,MAAM,IAAI,GAAG,iCAAiC,CAAC,GAAG,CAAC,cAAc,CAAC,OAAO,CAAC,QAAQ,IAAI,aAAa,CAAC,EAAE,MAAM,CAAC,CAAC;IAC9G,OAAO,YAAY,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;AACxG,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type CSSProperties, type HTMLAttributes, type Ref } from 'react';
|
|
2
|
+
import { type SanitizeHook } from '../html/sanitize.js';
|
|
3
|
+
import type { Block } from '../blocks/types.js';
|
|
4
|
+
export interface RichTextContentProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'dangerouslySetInnerHTML'> {
|
|
5
|
+
/** Stored HTML (for example a RichTextEditor value). Sanitized before rendering. */
|
|
6
|
+
value?: string;
|
|
7
|
+
/** BlockEditor blocks; rendered through `blocksToHtml` when `value` is not given. */
|
|
8
|
+
blocks?: readonly Block[];
|
|
9
|
+
/** Hostnames whose iframe embeds are kept (see `SanitizeHtmlOptions.embedHosts`). */
|
|
10
|
+
embedHosts?: readonly string[];
|
|
11
|
+
/** Further sanitizer restriction (see `SanitizeHook`); keep it stable (module scope or memoized). */
|
|
12
|
+
sanitize?: SanitizeHook;
|
|
13
|
+
className?: string;
|
|
14
|
+
style?: CSSProperties;
|
|
15
|
+
ref?: Ref<HTMLDivElement>;
|
|
16
|
+
}
|
|
17
|
+
/** Read-only rendering of rich text content; root class `salmon-rich-text-content`. */
|
|
18
|
+
export declare function RichTextContent(props: RichTextContentProps): import("react").JSX.Element;
|
|
19
|
+
//# sourceMappingURL=RichTextContent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RichTextContent.d.ts","sourceRoot":"","sources":["../../src/content/RichTextContent.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAW,KAAK,aAAa,EAAE,KAAK,cAAc,EAAE,KAAK,GAAG,EAAE,MAAM,OAAO,CAAC;AACnF,OAAO,EAAgB,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEtE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEhD,MAAM,WAAW,oBAAqB,SAAQ,IAAI,CAAC,cAAc,CAAC,cAAc,CAAC,EAAE,UAAU,GAAG,yBAAyB,CAAC;IACxH,oFAAoF;IACpF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qFAAqF;IACrF,MAAM,CAAC,EAAE,SAAS,KAAK,EAAE,CAAC;IAC1B,qFAAqF;IACrF,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC/B,qGAAqG;IACrG,QAAQ,CAAC,EAAE,YAAY,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,GAAG,CAAC,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;CAC3B;AAED,uFAAuF;AACvF,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,+BAgB1D"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
// RichTextContent: renders
|
|
3
|
+
// stored HTML (or BlockEditor blocks) read-only. The markup is computed by the
|
|
4
|
+
// pure, DOM-free sanitizer, so server and client render the same string and
|
|
5
|
+
// hydration never differs. No ProseMirror is loaded.
|
|
6
|
+
import { useMemo } from 'react';
|
|
7
|
+
import { sanitizeHtml } from '../html/sanitize.js';
|
|
8
|
+
import { blocksToHtml } from '../blocks/to-html.js';
|
|
9
|
+
/** Read-only rendering of rich text content; root class `salmon-rich-text-content`. */
|
|
10
|
+
export function RichTextContent(props) {
|
|
11
|
+
const { value, blocks, embedHosts, sanitize, className, ref, ...rest } = props;
|
|
12
|
+
const hostsKey = embedHosts ? embedHosts.join('\n') : '';
|
|
13
|
+
const html = useMemo(() => {
|
|
14
|
+
const source = value ?? (blocks ? blocksToHtml(blocks) : '');
|
|
15
|
+
const hosts = hostsKey ? hostsKey.split('\n') : [];
|
|
16
|
+
return sanitizeHtml(source, { embedHosts: hosts, ...(sanitize ? { sanitize } : {}) });
|
|
17
|
+
}, [value, blocks, hostsKey, sanitize]);
|
|
18
|
+
return (_jsx("div", { ...rest, ref: ref, className: className ? `salmon-rich-text-content ${className}` : 'salmon-rich-text-content', dangerouslySetInnerHTML: { __html: html } }));
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=RichTextContent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RichTextContent.js","sourceRoot":"","sources":["../../src/content/RichTextContent.tsx"],"names":[],"mappings":";AAAA,2BAA2B;AAC3B,+EAA+E;AAC/E,4EAA4E;AAC5E,qDAAqD;AAErD,OAAO,EAAE,OAAO,EAAqD,MAAM,OAAO,CAAC;AACnF,OAAO,EAAE,YAAY,EAAqB,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAiBpD,uFAAuF;AACvF,MAAM,UAAU,eAAe,CAAC,KAA2B;IACzD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,KAAK,CAAC;IAC/E,MAAM,QAAQ,GAAG,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACzD,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,EAAE;QACxB,MAAM,MAAM,GAAG,KAAK,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAC7D,MAAM,KAAK,GAAG,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACnD,OAAO,YAAY,CAAC,MAAM,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACxF,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC;IACxC,OAAO,CACL,iBACM,IAAI,EACR,GAAG,EAAE,GAAG,EACR,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,4BAA4B,SAAS,EAAE,CAAC,CAAC,CAAC,0BAA0B,EAC3F,uBAAuB,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,GACzC,CACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/content/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,KAAK,oBAAoB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/content/index.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,OAAO,EAAE,eAAe,EAA6B,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Command, type EditorState } from 'prosemirror-state';
|
|
2
|
+
import { Slice, type MarkType, type Node as PMNode, type Schema } from 'prosemirror-model';
|
|
3
|
+
import type { RichTextCommands } from './types.js';
|
|
4
|
+
export interface CommandContext {
|
|
5
|
+
readonly schema: Schema;
|
|
6
|
+
state(): EditorState;
|
|
7
|
+
exec(command: Command): boolean;
|
|
8
|
+
parseSlice(html: string): Slice;
|
|
9
|
+
}
|
|
10
|
+
export declare function safeHref(href: string): boolean;
|
|
11
|
+
export declare function markActive(state: EditorState, type: MarkType): boolean;
|
|
12
|
+
export declare function markValue(state: EditorState, type: MarkType): string | null;
|
|
13
|
+
export declare function ancestor(state: EditorState, test: (node: PMNode) => boolean): {
|
|
14
|
+
node: PMNode;
|
|
15
|
+
pos: number;
|
|
16
|
+
depth: number;
|
|
17
|
+
} | null;
|
|
18
|
+
export declare function createCommands(ctx: CommandContext): RichTextCommands;
|
|
19
|
+
//# sourceMappingURL=commands.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../../src/engine/commands.ts"],"names":[],"mappings":"AAMA,OAAO,EAAgC,KAAK,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEjG,OAAO,EAAE,KAAK,EAAa,KAAK,QAAQ,EAAE,KAAK,IAAI,IAAI,MAAM,EAAmC,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEvI,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,KAAK,IAAI,WAAW,CAAC;IACrB,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC;IAChC,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;CACjC;AAID,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAG9C;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAItE;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,CAI3E;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAOjI;AAqID,wBAAgB,cAAc,CAAC,GAAG,EAAE,cAAc,GAAG,gBAAgB,CA4PpE"}
|
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
// Built-in commands over ProseMirror
|
|
2
|
+
// transactions. They run headless: `exec` applies a command to the engine
|
|
3
|
+
// state with or without a view.
|
|
4
|
+
import { lift, selectAll, setBlockType, toggleMark, wrapIn } from 'prosemirror-commands';
|
|
5
|
+
import { redo } from 'prosemirror-history';
|
|
6
|
+
import { liftListItem, sinkListItem, wrapInList } from 'prosemirror-schema-list';
|
|
7
|
+
import { NodeSelection, TextSelection } from 'prosemirror-state';
|
|
8
|
+
import { addColumnAfter, addColumnBefore, addRowAfter, addRowBefore, deleteColumn, deleteRow, deleteTable, isInTable, mergeCells, splitCell, toggleHeaderRow } from 'prosemirror-tables';
|
|
9
|
+
import { Slice } from 'prosemirror-model';
|
|
10
|
+
import { applyPainter, findKey, findMatches, limitedUndo, painterKey } from './plugins.js';
|
|
11
|
+
const SAFE_URL = /^(?:https?:|mailto:|tel:|#|\/|\.{0,2}\/|[^:]*$)/i;
|
|
12
|
+
export function safeHref(href) {
|
|
13
|
+
const trimmed = href.trim();
|
|
14
|
+
return trimmed !== '' && SAFE_URL.test(trimmed) && !/^\s*(javascript|vbscript|data):/i.test(trimmed);
|
|
15
|
+
}
|
|
16
|
+
export function markActive(state, type) {
|
|
17
|
+
const { from, $from, to, empty } = state.selection;
|
|
18
|
+
if (empty)
|
|
19
|
+
return Boolean(type.isInSet(state.storedMarks ?? $from.marks()));
|
|
20
|
+
return state.doc.rangeHasMark(from, to, type);
|
|
21
|
+
}
|
|
22
|
+
export function markValue(state, type) {
|
|
23
|
+
const { $from, empty } = state.selection;
|
|
24
|
+
const marks = empty ? (state.storedMarks ?? $from.marks()) : ($from.nodeAfter?.marks ?? $from.marks());
|
|
25
|
+
return type.isInSet(marks)?.attrs['value'] ?? null;
|
|
26
|
+
}
|
|
27
|
+
export function ancestor(state, test) {
|
|
28
|
+
const $from = state.selection.$from;
|
|
29
|
+
for (let d = $from.depth; d > 0; d--) {
|
|
30
|
+
const node = $from.node(d);
|
|
31
|
+
if (test(node))
|
|
32
|
+
return { node, pos: $from.before(d), depth: d };
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
function linkRange($pos, type) {
|
|
37
|
+
const parent = $pos.parent;
|
|
38
|
+
const start = $pos.parentOffset;
|
|
39
|
+
let index = $pos.index();
|
|
40
|
+
let child = parent.maybeChild(index);
|
|
41
|
+
if ((!child || !type.isInSet(child.marks)) && index > 0)
|
|
42
|
+
child = parent.maybeChild(--index);
|
|
43
|
+
if (!child)
|
|
44
|
+
return null;
|
|
45
|
+
const mark = type.isInSet(child.marks);
|
|
46
|
+
if (!mark)
|
|
47
|
+
return null;
|
|
48
|
+
let from = $pos.start();
|
|
49
|
+
for (let i = 0; i < index; i++)
|
|
50
|
+
from += parent.child(i).nodeSize;
|
|
51
|
+
let s = index;
|
|
52
|
+
let fromPos = from;
|
|
53
|
+
while (s > 0 && mark.isInSet(parent.child(s - 1).marks))
|
|
54
|
+
fromPos -= parent.child(--s).nodeSize;
|
|
55
|
+
let e = index;
|
|
56
|
+
let toPos = from + parent.child(index).nodeSize;
|
|
57
|
+
while (e + 1 < parent.childCount && mark.isInSet(parent.child(e + 1).marks))
|
|
58
|
+
toPos += parent.child(++e).nodeSize;
|
|
59
|
+
void start;
|
|
60
|
+
return { from: fromPos, to: toPos };
|
|
61
|
+
}
|
|
62
|
+
function textblocks(state, visit) {
|
|
63
|
+
const { from, to } = state.selection;
|
|
64
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
65
|
+
if (node.isTextblock) {
|
|
66
|
+
visit(node, pos);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function setTextAttr(name, value) {
|
|
73
|
+
return (state, dispatch) => {
|
|
74
|
+
const tr = state.tr;
|
|
75
|
+
textblocks(state, (node, pos) => {
|
|
76
|
+
if (name in node.attrs)
|
|
77
|
+
tr.setNodeMarkup(pos, undefined, { ...node.attrs, [name]: value });
|
|
78
|
+
});
|
|
79
|
+
if (!tr.docChanged)
|
|
80
|
+
return false;
|
|
81
|
+
dispatch?.(tr);
|
|
82
|
+
return true;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function styleMark(type, value) {
|
|
86
|
+
return (state, dispatch) => {
|
|
87
|
+
const { from, to, empty } = state.selection;
|
|
88
|
+
const tr = state.tr;
|
|
89
|
+
if (empty) {
|
|
90
|
+
tr.removeStoredMark(type);
|
|
91
|
+
if (value)
|
|
92
|
+
tr.addStoredMark(type.create({ value }));
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
tr.removeMark(from, to, type);
|
|
96
|
+
if (value)
|
|
97
|
+
tr.addMark(from, to, type.create({ value }));
|
|
98
|
+
}
|
|
99
|
+
dispatch?.(tr);
|
|
100
|
+
return true;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function changeCase(upper) {
|
|
104
|
+
return (state, dispatch) => {
|
|
105
|
+
const { from, to, empty } = state.selection;
|
|
106
|
+
if (empty)
|
|
107
|
+
return false;
|
|
108
|
+
const tr = state.tr;
|
|
109
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
110
|
+
if (!node.isText)
|
|
111
|
+
return true;
|
|
112
|
+
const start = Math.max(from, pos);
|
|
113
|
+
const end = Math.min(to, pos + node.nodeSize);
|
|
114
|
+
const text = node.text.slice(start - pos, end - pos);
|
|
115
|
+
const next = upper ? text.toUpperCase() : text.toLowerCase();
|
|
116
|
+
if (next !== text)
|
|
117
|
+
tr.replaceWith(tr.mapping.map(start), tr.mapping.map(end), state.schema.text(next, node.marks));
|
|
118
|
+
return false;
|
|
119
|
+
});
|
|
120
|
+
tr.setSelection(TextSelection.create(tr.doc, from, tr.mapping.map(to)));
|
|
121
|
+
dispatch?.(tr);
|
|
122
|
+
return true;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function listAncestor(state) {
|
|
126
|
+
return ancestor(state, (n) => n.type.name === 'bullet_list' || n.type.name === 'ordered_list' || n.type.name === 'task_list');
|
|
127
|
+
}
|
|
128
|
+
function toggleList(ctx, typeName, style) {
|
|
129
|
+
const { schema } = ctx;
|
|
130
|
+
const type = schema.nodes[typeName];
|
|
131
|
+
const item = schema.nodes[typeName === 'task_list' ? 'task_item' : 'list_item'];
|
|
132
|
+
const list = listAncestor(ctx.state());
|
|
133
|
+
if (list) {
|
|
134
|
+
if (list.node.type === type) {
|
|
135
|
+
if (style !== undefined && style !== null && list.node.attrs['listStyle'] !== style) {
|
|
136
|
+
return ctx.exec((state, dispatch) => (dispatch?.(state.tr.setNodeMarkup(list.pos, undefined, { ...list.node.attrs, listStyle: style })), true));
|
|
137
|
+
}
|
|
138
|
+
return ctx.exec(liftListItem(item));
|
|
139
|
+
}
|
|
140
|
+
const sameItem = list.node.type.contentMatch.defaultType === item;
|
|
141
|
+
if (sameItem) {
|
|
142
|
+
return ctx.exec((state, dispatch) => (dispatch?.(state.tr.setNodeMarkup(list.pos, type, { ...(type.name === 'task_list' ? {} : { listStyle: style ?? null }), blockId: list.node.attrs['blockId'] ?? null })), true));
|
|
143
|
+
}
|
|
144
|
+
ctx.exec(liftListItem(list.node.type.contentMatch.defaultType));
|
|
145
|
+
}
|
|
146
|
+
return ctx.exec(wrapInList(type, typeName === 'task_list' ? null : { listStyle: style ?? null }));
|
|
147
|
+
}
|
|
148
|
+
function insertNode(node) {
|
|
149
|
+
return (state, dispatch) => {
|
|
150
|
+
const tr = state.tr.replaceSelectionWith(node);
|
|
151
|
+
// After a block atom the caret goes into the next text block (one is added at the end).
|
|
152
|
+
if (!node.isInline && tr.selection instanceof NodeSelection) {
|
|
153
|
+
const end = tr.selection.to;
|
|
154
|
+
if (!tr.doc.resolve(end).nodeAfter?.isTextblock)
|
|
155
|
+
tr.insert(end, state.schema.nodes['paragraph'].create());
|
|
156
|
+
tr.setSelection(TextSelection.create(tr.doc, end + 1));
|
|
157
|
+
}
|
|
158
|
+
dispatch?.(tr.scrollIntoView());
|
|
159
|
+
return true;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function find(ctx) {
|
|
163
|
+
return findKey.getState(ctx.state());
|
|
164
|
+
}
|
|
165
|
+
function selectMatch(ctx, index) {
|
|
166
|
+
const state = find(ctx);
|
|
167
|
+
if (!state || !state.matches.length)
|
|
168
|
+
return false;
|
|
169
|
+
const i = (index + state.matches.length) % state.matches.length;
|
|
170
|
+
const m = state.matches[i];
|
|
171
|
+
return ctx.exec((s, dispatch) => (dispatch?.(s.tr.setSelection(TextSelection.create(s.doc, m.from, m.to)).setMeta(findKey, { index: i, matches: state.matches }).scrollIntoView()), true));
|
|
172
|
+
}
|
|
173
|
+
export function createCommands(ctx) {
|
|
174
|
+
const { schema } = ctx;
|
|
175
|
+
const m = schema.marks;
|
|
176
|
+
const n = schema.nodes;
|
|
177
|
+
const mark = (name) => () => (m[name] ? ctx.exec(toggleMark(m[name])) : false);
|
|
178
|
+
const blockType = (type, attrs) => ctx.exec((state, dispatch) => {
|
|
179
|
+
const tr = state.tr;
|
|
180
|
+
textblocks(state, (node, pos) => {
|
|
181
|
+
if (node.type.spec.code && type !== node.type)
|
|
182
|
+
return;
|
|
183
|
+
const keep = 'textAlign' in node.attrs ? { textAlign: node.attrs['textAlign'], lineHeight: node.attrs['lineHeight'], indent: node.attrs['indent'] } : {};
|
|
184
|
+
const next = { ...(type.spec.code ? {} : keep), ...attrs, blockId: node.attrs['blockId'] ?? null };
|
|
185
|
+
if (type.validContent(node.content) || type.spec.code)
|
|
186
|
+
tr.setBlockType(pos, pos + node.nodeSize, type, next);
|
|
187
|
+
});
|
|
188
|
+
if (!tr.docChanged)
|
|
189
|
+
return setBlockType(type, attrs)(state, dispatch);
|
|
190
|
+
dispatch?.(tr);
|
|
191
|
+
return true;
|
|
192
|
+
});
|
|
193
|
+
const inParent = (name) => ctx.state().selection.$from.parent.type.name === name;
|
|
194
|
+
const cmds = {
|
|
195
|
+
bold: mark('bold'),
|
|
196
|
+
italic: mark('italic'),
|
|
197
|
+
underline: mark('underline'),
|
|
198
|
+
strike: mark('strike'),
|
|
199
|
+
subscript: mark('subscript'),
|
|
200
|
+
superscript: mark('superscript'),
|
|
201
|
+
code: mark('code'),
|
|
202
|
+
uppercase: () => ctx.exec(changeCase(true)),
|
|
203
|
+
lowercase: () => ctx.exec(changeCase(false)),
|
|
204
|
+
fontFamily: (value) => ctx.exec(styleMark(m['fontFamily'], value)),
|
|
205
|
+
fontSize: (value) => ctx.exec(styleMark(m['fontSize'], value)),
|
|
206
|
+
color: (value) => ctx.exec(styleMark(m['color'], value)),
|
|
207
|
+
backgroundColor: (value) => ctx.exec(styleMark(m['backgroundColor'], value)),
|
|
208
|
+
paragraph: () => blockType(n['paragraph'], {}),
|
|
209
|
+
heading: (level) => {
|
|
210
|
+
const parent = ctx.state().selection.$from.parent;
|
|
211
|
+
if (parent.type === n['heading'] && parent.attrs['level'] === level)
|
|
212
|
+
return blockType(n['paragraph'], {});
|
|
213
|
+
return blockType(n['heading'], { level });
|
|
214
|
+
},
|
|
215
|
+
blockquote: () => (ancestor(ctx.state(), (node) => node.type === n['blockquote']) ? ctx.exec(lift) : ctx.exec(wrapIn(n['blockquote']))),
|
|
216
|
+
codeBlock: (language) => (inParent('code_block') ? blockType(n['paragraph'], {}) : blockType(n['code_block'], { language: language ?? null })),
|
|
217
|
+
setCodeLanguage: (language) => {
|
|
218
|
+
const block = ancestor(ctx.state(), (node) => node.type === n['code_block']);
|
|
219
|
+
if (!block)
|
|
220
|
+
return false;
|
|
221
|
+
return ctx.exec((state, dispatch) => (dispatch?.(state.tr.setNodeMarkup(block.pos, undefined, { ...block.node.attrs, language })), true));
|
|
222
|
+
},
|
|
223
|
+
textAlign: (align) => ctx.exec(setTextAttr('textAlign', align)),
|
|
224
|
+
lineHeight: (value) => ctx.exec(setTextAttr('lineHeight', value)),
|
|
225
|
+
indent: () => {
|
|
226
|
+
const state = ctx.state();
|
|
227
|
+
const item = ancestor(state, (node) => node.type === n['list_item'] || node.type === n['task_item']);
|
|
228
|
+
if (item)
|
|
229
|
+
return ctx.exec(sinkListItem(item.node.type));
|
|
230
|
+
const parent = state.selection.$from.parent;
|
|
231
|
+
return ctx.exec(setTextAttr('indent', Math.min(8, (parent.attrs['indent'] ?? 0) + 1)));
|
|
232
|
+
},
|
|
233
|
+
outdent: () => {
|
|
234
|
+
const state = ctx.state();
|
|
235
|
+
const item = ancestor(state, (node) => node.type === n['list_item'] || node.type === n['task_item']);
|
|
236
|
+
if (item)
|
|
237
|
+
return ctx.exec(liftListItem(item.node.type));
|
|
238
|
+
const indent = state.selection.$from.parent.attrs['indent'] ?? 0;
|
|
239
|
+
return indent > 0 ? ctx.exec(setTextAttr('indent', indent - 1)) : false;
|
|
240
|
+
},
|
|
241
|
+
bulletList: (style) => toggleList(ctx, 'bullet_list', style),
|
|
242
|
+
orderedList: (style) => toggleList(ctx, 'ordered_list', style),
|
|
243
|
+
taskList: () => toggleList(ctx, 'task_list', undefined),
|
|
244
|
+
toggleTask: () => {
|
|
245
|
+
const item = ancestor(ctx.state(), (node) => node.type === n['task_item']);
|
|
246
|
+
if (!item)
|
|
247
|
+
return false;
|
|
248
|
+
return ctx.exec((state, dispatch) => (dispatch?.(state.tr.setNodeMarkup(item.pos, undefined, { ...item.node.attrs, checked: !item.node.attrs['checked'] })), true));
|
|
249
|
+
},
|
|
250
|
+
horizontalRule: () => ctx.exec(insertNode(n['horizontal_rule'].create())),
|
|
251
|
+
clearFormat: () => ctx.exec((state, dispatch) => {
|
|
252
|
+
const { from, to } = state.selection;
|
|
253
|
+
const tr = state.tr;
|
|
254
|
+
for (const [name, type] of Object.entries(m))
|
|
255
|
+
if (name !== 'link')
|
|
256
|
+
tr.removeMark(from, to, type);
|
|
257
|
+
if (state.selection.empty)
|
|
258
|
+
tr.setStoredMarks([]);
|
|
259
|
+
textblocks(state, (node, pos) => {
|
|
260
|
+
if (node.type.spec.code)
|
|
261
|
+
return;
|
|
262
|
+
tr.setNodeMarkup(pos, n['paragraph'], { blockId: node.attrs['blockId'] ?? null });
|
|
263
|
+
});
|
|
264
|
+
dispatch?.(tr);
|
|
265
|
+
return true;
|
|
266
|
+
}),
|
|
267
|
+
setLink: (link) => {
|
|
268
|
+
if (!safeHref(link.href))
|
|
269
|
+
return false;
|
|
270
|
+
const type = m['link'];
|
|
271
|
+
const attrs = { href: link.href.trim(), title: link.title || null, target: link.target === '_blank' ? '_blank' : null };
|
|
272
|
+
return ctx.exec((state, dispatch) => {
|
|
273
|
+
const { from, to, empty, $from } = state.selection;
|
|
274
|
+
const tr = state.tr;
|
|
275
|
+
if (empty) {
|
|
276
|
+
const range = linkRange($from, type);
|
|
277
|
+
if (range) {
|
|
278
|
+
if (link.text !== undefined && link.text !== state.doc.textBetween(range.from, range.to)) {
|
|
279
|
+
tr.insertText(link.text, range.from, range.to);
|
|
280
|
+
tr.addMark(range.from, range.from + link.text.length, type.create(attrs));
|
|
281
|
+
}
|
|
282
|
+
else
|
|
283
|
+
tr.removeMark(range.from, range.to, type).addMark(range.from, range.to, type.create(attrs));
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
const text = link.text || link.href;
|
|
287
|
+
tr.insert(from, schema.text(text, [...$from.marks().filter((mk) => mk.type !== type), type.create(attrs)]));
|
|
288
|
+
tr.setSelection(TextSelection.create(tr.doc, from + text.length));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
if (link.text !== undefined && link.text !== '' && link.text !== state.doc.textBetween(from, to)) {
|
|
293
|
+
tr.insertText(link.text, from, to);
|
|
294
|
+
tr.addMark(from, from + link.text.length, type.create(attrs));
|
|
295
|
+
}
|
|
296
|
+
else
|
|
297
|
+
tr.removeMark(from, to, type).addMark(from, to, type.create(attrs));
|
|
298
|
+
}
|
|
299
|
+
dispatch?.(tr);
|
|
300
|
+
return true;
|
|
301
|
+
});
|
|
302
|
+
},
|
|
303
|
+
unsetLink: () => ctx.exec((state, dispatch) => {
|
|
304
|
+
const type = m['link'];
|
|
305
|
+
const { from, to, empty, $from } = state.selection;
|
|
306
|
+
const range = empty ? linkRange($from, type) : { from, to };
|
|
307
|
+
if (!range || !state.doc.rangeHasMark(range.from, range.to, type))
|
|
308
|
+
return false;
|
|
309
|
+
dispatch?.(state.tr.removeMark(range.from, range.to, type));
|
|
310
|
+
return true;
|
|
311
|
+
}),
|
|
312
|
+
insertImage: (image) => {
|
|
313
|
+
const attrs = { src: image.src, alt: image.alt ?? '', title: image.title ?? null, width: image.width ?? null, height: image.height ?? null };
|
|
314
|
+
if (image.caption)
|
|
315
|
+
return ctx.exec(insertNode(n['figure'].create(attrs, schema.text(image.caption))));
|
|
316
|
+
return ctx.exec(insertNode(n['image'].create(attrs)));
|
|
317
|
+
},
|
|
318
|
+
updateImage: (image) => ctx.exec((state, dispatch) => {
|
|
319
|
+
const sel = state.selection;
|
|
320
|
+
let pos = -1;
|
|
321
|
+
let node = null;
|
|
322
|
+
if (sel instanceof NodeSelection && (sel.node.type === n['image'] || sel.node.type === n['figure'])) {
|
|
323
|
+
pos = sel.from;
|
|
324
|
+
node = sel.node;
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
const fig = ancestor(state, (x) => x.type === n['figure']);
|
|
328
|
+
if (fig)
|
|
329
|
+
({ pos, node } = fig);
|
|
330
|
+
}
|
|
331
|
+
if (!node)
|
|
332
|
+
return false;
|
|
333
|
+
const { caption, ...rest } = image;
|
|
334
|
+
const attrs = { ...node.attrs, ...Object.fromEntries(Object.entries(rest).filter(([, v]) => v !== undefined)) };
|
|
335
|
+
const tr = state.tr.setNodeMarkup(pos, undefined, attrs);
|
|
336
|
+
if (sel instanceof NodeSelection)
|
|
337
|
+
tr.setSelection(NodeSelection.create(tr.doc, pos));
|
|
338
|
+
dispatch?.(tr);
|
|
339
|
+
void caption;
|
|
340
|
+
return true;
|
|
341
|
+
}),
|
|
342
|
+
insertAudio: (media) => ctx.exec(insertNode(n['audio'].create({ src: media.src }))),
|
|
343
|
+
insertVideo: (media) => ctx.exec(insertNode(n['video'].create({ src: media.src, width: media.width ?? null, height: media.height ?? null }))),
|
|
344
|
+
insertEmbed: (media) => ctx.exec(insertNode(n['embed'].create({ src: media.src, width: media.width ?? null, height: media.height ?? null }))),
|
|
345
|
+
deleteNode: () => ctx.exec((state, dispatch) => {
|
|
346
|
+
if (!(state.selection instanceof NodeSelection))
|
|
347
|
+
return false;
|
|
348
|
+
dispatch?.(state.tr.deleteSelection());
|
|
349
|
+
return true;
|
|
350
|
+
}),
|
|
351
|
+
insertTable: ({ rows, cols, headerRow }) => {
|
|
352
|
+
const r = Math.max(1, Math.min(50, Math.floor(rows)));
|
|
353
|
+
const c = Math.max(1, Math.min(20, Math.floor(cols)));
|
|
354
|
+
const row = (header) => n['table_row'].create(null, Array.from({ length: c }, () => n[header ? 'table_header' : 'table_cell'].createAndFill()));
|
|
355
|
+
const table = n['table'].create(null, Array.from({ length: r }, (_, i) => row(Boolean(headerRow) && i === 0)));
|
|
356
|
+
return ctx.exec((state, dispatch) => {
|
|
357
|
+
const tr = state.tr.replaceSelectionWith(table);
|
|
358
|
+
// Caret into the first cell.
|
|
359
|
+
const start = tr.selection.from;
|
|
360
|
+
let pos = -1;
|
|
361
|
+
tr.doc.nodesBetween(Math.max(0, start - table.nodeSize - 2), Math.min(tr.doc.content.size, start + 2), (node, p) => {
|
|
362
|
+
if (pos < 0 && node === table)
|
|
363
|
+
pos = p;
|
|
364
|
+
return pos < 0;
|
|
365
|
+
});
|
|
366
|
+
if (pos >= 0)
|
|
367
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(pos + 4)));
|
|
368
|
+
dispatch?.(tr.scrollIntoView());
|
|
369
|
+
return true;
|
|
370
|
+
});
|
|
371
|
+
},
|
|
372
|
+
addRowBefore: () => ctx.exec(addRowBefore),
|
|
373
|
+
addRowAfter: () => ctx.exec(addRowAfter),
|
|
374
|
+
deleteRow: () => ctx.exec(deleteRow),
|
|
375
|
+
addColumnBefore: () => ctx.exec(addColumnBefore),
|
|
376
|
+
addColumnAfter: () => ctx.exec(addColumnAfter),
|
|
377
|
+
deleteColumn: () => ctx.exec(deleteColumn),
|
|
378
|
+
mergeCells: () => ctx.exec(mergeCells),
|
|
379
|
+
splitCell: () => ctx.exec(splitCell),
|
|
380
|
+
toggleHeaderRow: () => ctx.exec(toggleHeaderRow),
|
|
381
|
+
setTableStyle: (style) => {
|
|
382
|
+
if (!isInTable(ctx.state()))
|
|
383
|
+
return false;
|
|
384
|
+
const table = ancestor(ctx.state(), (node) => node.type === n['table']);
|
|
385
|
+
if (!table)
|
|
386
|
+
return false;
|
|
387
|
+
return ctx.exec((state, dispatch) => (dispatch?.(state.tr.setNodeMarkup(table.pos, undefined, { ...table.node.attrs, variant: style })), true));
|
|
388
|
+
},
|
|
389
|
+
deleteTable: () => ctx.exec(deleteTable),
|
|
390
|
+
insertText: (text) => ctx.exec((state, dispatch) => (dispatch?.(state.tr.insertText(text).scrollIntoView()), true)),
|
|
391
|
+
insertHtml: (html, options) => {
|
|
392
|
+
const parsed = ctx.parseSlice(html);
|
|
393
|
+
const slice = options?.blocks ? new Slice(parsed.content, 0, 0) : parsed;
|
|
394
|
+
return ctx.exec((state, dispatch) => (dispatch?.(state.tr.replaceSelection(slice).scrollIntoView()), true));
|
|
395
|
+
},
|
|
396
|
+
selectAll: () => ctx.exec(selectAll),
|
|
397
|
+
undo: () => ctx.exec(limitedUndo),
|
|
398
|
+
redo: () => ctx.exec(redo),
|
|
399
|
+
find: (query, options = {}) => {
|
|
400
|
+
ctx.exec((state, dispatch) => (dispatch?.(state.tr.setMeta(findKey, { query, options, matches: findMatches(state.doc, query, options) })), true));
|
|
401
|
+
const found = find(ctx);
|
|
402
|
+
if (found?.matches.length)
|
|
403
|
+
selectMatch(ctx, 0);
|
|
404
|
+
return found?.matches.length ?? 0;
|
|
405
|
+
},
|
|
406
|
+
findNext: () => selectMatch(ctx, (find(ctx)?.index ?? -1) + 1),
|
|
407
|
+
findPrevious: () => selectMatch(ctx, (find(ctx)?.index ?? 1) - 1),
|
|
408
|
+
replace: (text) => {
|
|
409
|
+
const state = find(ctx);
|
|
410
|
+
if (!state || state.index < 0)
|
|
411
|
+
return false;
|
|
412
|
+
const match = state.matches[state.index];
|
|
413
|
+
const index = state.index;
|
|
414
|
+
ctx.exec((s, dispatch) => (dispatch?.(s.tr.insertText(text, match.from, match.to)), true));
|
|
415
|
+
const after = find(ctx);
|
|
416
|
+
if (after?.matches.length)
|
|
417
|
+
selectMatch(ctx, Math.min(index, after.matches.length - 1));
|
|
418
|
+
return true;
|
|
419
|
+
},
|
|
420
|
+
replaceAll: (text) => {
|
|
421
|
+
const state = find(ctx);
|
|
422
|
+
if (!state || !state.matches.length)
|
|
423
|
+
return 0;
|
|
424
|
+
const count = state.matches.length;
|
|
425
|
+
ctx.exec((s, dispatch) => {
|
|
426
|
+
const tr = s.tr;
|
|
427
|
+
for (let i = state.matches.length - 1; i >= 0; i--)
|
|
428
|
+
tr.insertText(text, state.matches[i].from, state.matches[i].to);
|
|
429
|
+
dispatch?.(tr);
|
|
430
|
+
return true;
|
|
431
|
+
});
|
|
432
|
+
return count;
|
|
433
|
+
},
|
|
434
|
+
clearFind: () => ctx.exec((state, dispatch) => (dispatch?.(state.tr.setMeta(findKey, null)), true)),
|
|
435
|
+
copyFormat: (sticky = false) => ctx.exec((state, dispatch) => {
|
|
436
|
+
const { $from } = state.selection;
|
|
437
|
+
const marks = state.selection.empty ? (state.storedMarks ?? $from.marks()) : ($from.nodeAfter?.marks ?? $from.marks());
|
|
438
|
+
const parent = $from.parent;
|
|
439
|
+
const block = parent.isTextblock && !parent.type.spec.code ? { type: parent.type, attrs: { ...parent.attrs } } : null;
|
|
440
|
+
dispatch?.(state.tr.setMeta(painterKey, { marks, block, sticky }));
|
|
441
|
+
return true;
|
|
442
|
+
}),
|
|
443
|
+
applyFormat: () => {
|
|
444
|
+
const painter = painterKey.getState(ctx.state());
|
|
445
|
+
if (!painter || ctx.state().selection.empty)
|
|
446
|
+
return false;
|
|
447
|
+
return ctx.exec((state, dispatch) => (dispatch?.(applyPainter(state, painter)), true));
|
|
448
|
+
},
|
|
449
|
+
cancelFormatPainter: () => {
|
|
450
|
+
if (!painterKey.getState(ctx.state()))
|
|
451
|
+
return false;
|
|
452
|
+
return ctx.exec((state, dispatch) => (dispatch?.(state.tr.setMeta(painterKey, null)), true));
|
|
453
|
+
},
|
|
454
|
+
};
|
|
455
|
+
return cmds;
|
|
456
|
+
}
|
|
457
|
+
//# sourceMappingURL=commands.js.map
|