@prosekit/extensions 0.11.5 → 0.11.6
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/drop-indicator-E7nCfdnR.js +58 -0
- package/dist/drop-indicator-E7nCfdnR.js.map +1 -0
- package/dist/file-DVUhe5KJ.js +134 -0
- package/dist/file-DVUhe5KJ.js.map +1 -0
- package/dist/index-DY6lIIYV.d.ts +134 -0
- package/dist/index-DY6lIIYV.d.ts.map +1 -0
- package/dist/{mark-rule-BCqIZMDu.js → mark-rule-CGmswjQ_.js} +1 -1
- package/dist/{mark-rule-BCqIZMDu.js.map → mark-rule-CGmswjQ_.js.map} +1 -1
- package/dist/{paste-rule-DIEJKIje.js → paste-rule-BIztzELg.js} +1 -1
- package/dist/{paste-rule-DIEJKIje.js.map → paste-rule-BIztzELg.js.map} +1 -1
- package/dist/prosekit-extensions-code-block.d.ts +1 -1
- package/dist/prosekit-extensions-drop-indicator.d.ts +3 -106
- package/dist/prosekit-extensions-drop-indicator.d.ts.map +1 -1
- package/dist/prosekit-extensions-drop-indicator.js +1 -1
- package/dist/prosekit-extensions-file.d.ts +2 -126
- package/dist/prosekit-extensions-file.js +2 -128
- package/dist/prosekit-extensions-hard-break.d.ts +0 -4
- package/dist/prosekit-extensions-hard-break.d.ts.map +1 -1
- package/dist/prosekit-extensions-image.d.ts +79 -3
- package/dist/prosekit-extensions-image.d.ts.map +1 -1
- package/dist/prosekit-extensions-image.js +88 -8
- package/dist/prosekit-extensions-image.js.map +1 -1
- package/dist/prosekit-extensions-link.js +2 -2
- package/dist/prosekit-extensions-list.js +2 -2
- package/dist/prosekit-extensions-mark-rule.js +1 -1
- package/dist/prosekit-extensions-paragraph.d.ts +0 -4
- package/dist/prosekit-extensions-paragraph.d.ts.map +1 -1
- package/dist/prosekit-extensions-paste-rule.js +1 -1
- package/dist/prosekit-extensions-placeholder.js +2 -2
- package/dist/prosekit-extensions-table.js +2 -2
- package/dist/prosekit-extensions.d.ts +1 -1
- package/dist/{shiki-highlighter-chunk-DSPM0T27.d.ts → shiki-highlighter-chunk-Cwu1Jr9o.d.ts} +1 -1
- package/dist/{shiki-highlighter-chunk-DSPM0T27.d.ts.map → shiki-highlighter-chunk-Cwu1Jr9o.d.ts.map} +1 -1
- package/dist/shiki-highlighter-chunk.d.ts +1 -1
- package/dist/{table-Bi7WsMI3.js → table-BNwuK7xg.js} +2 -2
- package/dist/{table-Bi7WsMI3.js.map → table-BNwuK7xg.js.map} +1 -1
- package/package.json +7 -6
- package/src/drop-indicator/drop-indicator-facet.ts +6 -28
- package/src/drop-indicator/drop-indicator.ts +3 -5
- package/src/drop-indicator/index.ts +6 -6
- package/src/file/file-upload.ts +29 -8
- package/src/image/image-commands.ts +12 -3
- package/src/image/image-upload-handler.ts +156 -0
- package/src/image/index.ts +9 -0
- package/dist/drop-indicator-D1eHOhSi.js +0 -267
- package/dist/drop-indicator-D1eHOhSi.js.map +0 -1
- package/dist/prosekit-extensions-file.d.ts.map +0 -1
- package/dist/prosekit-extensions-file.js.map +0 -1
- package/src/drop-indicator/drop-indicator-plugin.ts +0 -147
- package/src/drop-indicator/drop-target.ts +0 -168
- package/src/drop-indicator/types.ts +0 -90
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { defineFacet, defineFacetPayload, pluginFacet } from "@prosekit/core";
|
|
2
|
+
import { createDropIndicatorPlugin } from "prosemirror-drop-indicator";
|
|
3
|
+
|
|
4
|
+
//#region src/drop-indicator/drop-indicator-facet.ts
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
function defineDropIndicatorPayload(payload) {
|
|
9
|
+
return defineFacetPayload(dropIndicatorFacet, [payload]);
|
|
10
|
+
}
|
|
11
|
+
const dropIndicatorFacet = defineFacet({
|
|
12
|
+
parent: pluginFacet,
|
|
13
|
+
singleton: true,
|
|
14
|
+
reducer: (payloads) => {
|
|
15
|
+
let showHandlers = payloads.map((p) => p.onShow).filter((x) => !!x);
|
|
16
|
+
let hideHandlers = payloads.map((p) => p.onHide).filter((x) => !!x);
|
|
17
|
+
let dragHandlers = payloads.map((p) => p.onDrag).filter((x) => !!x);
|
|
18
|
+
let showHandler = (options) => {
|
|
19
|
+
for (let fn of showHandlers) fn(options);
|
|
20
|
+
};
|
|
21
|
+
let hideHandler = () => {
|
|
22
|
+
for (let fn of hideHandlers) fn();
|
|
23
|
+
};
|
|
24
|
+
let dragHandler = (options) => {
|
|
25
|
+
for (let fn of dragHandlers) if (fn(options) === false) return false;
|
|
26
|
+
return true;
|
|
27
|
+
};
|
|
28
|
+
if (showHandlers.length === 0) return [];
|
|
29
|
+
return createDropIndicatorPlugin({
|
|
30
|
+
onDrag: dragHandler,
|
|
31
|
+
onShow: showHandler,
|
|
32
|
+
onHide: hideHandler
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/drop-indicator/drop-indicator.ts
|
|
39
|
+
/**
|
|
40
|
+
* Defines an extension that controls the behavior of the drop indicator.
|
|
41
|
+
*
|
|
42
|
+
* This extension itself doesn't draw the drop indicator, but it provides the
|
|
43
|
+
* necessary callbacks to do so. You probably don't want to use this extension
|
|
44
|
+
* directly, but rather use the `<DropIndicator>` component.
|
|
45
|
+
*
|
|
46
|
+
* You can add this extension multiple times. If any extension has `onDrag`
|
|
47
|
+
* callback defined, and it returns `false`, then the drop point will be
|
|
48
|
+
* discarded.
|
|
49
|
+
*
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
function defineDropIndicator(options) {
|
|
53
|
+
return defineDropIndicatorPayload(options ?? {});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
export { defineDropIndicator };
|
|
58
|
+
//# sourceMappingURL=drop-indicator-E7nCfdnR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drop-indicator-E7nCfdnR.js","names":["showHandler: ShowHandler","hideHandler: VoidFunction","dragHandler: DragEventHandler"],"sources":["../src/drop-indicator/drop-indicator-facet.ts","../src/drop-indicator/drop-indicator.ts"],"sourcesContent":["import {\n defineFacet,\n defineFacetPayload,\n pluginFacet,\n type PlainExtension,\n type PluginPayload,\n} from '@prosekit/core'\nimport type {\n DragEventHandler,\n DropIndicatorPluginOptions,\n ShowHandler,\n} from 'prosemirror-drop-indicator'\nimport { createDropIndicatorPlugin } from 'prosemirror-drop-indicator'\n\n/**\n * @internal\n */\nexport function defineDropIndicatorPayload(\n payload: DropIndicatorPluginOptions,\n): PlainExtension {\n return defineFacetPayload(dropIndicatorFacet, [payload]) as PlainExtension\n}\n\nconst dropIndicatorFacet = defineFacet<DropIndicatorPluginOptions, PluginPayload>({\n parent: pluginFacet,\n singleton: true,\n reducer: (payloads: DropIndicatorPluginOptions[]): PluginPayload => {\n let showHandlers = payloads.map(p => p.onShow).filter(x => !!x)\n let hideHandlers = payloads.map(p => p.onHide).filter(x => !!x)\n let dragHandlers = payloads.map(p => p.onDrag).filter(x => !!x)\n\n let showHandler: ShowHandler = (options) => {\n for (let fn of showHandlers) {\n fn(options)\n }\n }\n\n let hideHandler: VoidFunction = () => {\n for (let fn of hideHandlers) {\n fn()\n }\n }\n\n let dragHandler: DragEventHandler = (options): boolean => {\n for (let fn of dragHandlers) {\n if (fn(options) === false) return false\n }\n return true\n }\n\n if (showHandlers.length === 0) {\n // No `onShow` event handler, so we don't need to create a plugin.\n return []\n }\n\n return createDropIndicatorPlugin({\n onDrag: dragHandler,\n onShow: showHandler,\n onHide: hideHandler,\n })\n },\n})\n","import type { PlainExtension } from '@prosekit/core'\nimport type { DropIndicatorPluginOptions } from 'prosemirror-drop-indicator'\n\nimport { defineDropIndicatorPayload } from './drop-indicator-facet'\n\n/**\n * @internal\n */\nexport type DropIndicatorExtension = PlainExtension\n\n/**\n * Defines an extension that controls the behavior of the drop indicator.\n *\n * This extension itself doesn't draw the drop indicator, but it provides the\n * necessary callbacks to do so. You probably don't want to use this extension\n * directly, but rather use the `<DropIndicator>` component.\n *\n * You can add this extension multiple times. If any extension has `onDrag`\n * callback defined, and it returns `false`, then the drop point will be\n * discarded.\n *\n * @public\n */\nexport function defineDropIndicator(\n options?: DropIndicatorOptions,\n): DropIndicatorExtension {\n return defineDropIndicatorPayload(options ?? {})\n}\n\n/**\n * Options for {@link defineDropIndicator}.\n *\n * @public\n */\nexport interface DropIndicatorOptions extends DropIndicatorPluginOptions {}\n"],"mappings":";;;;;;;AAiBA,SAAgB,2BACd,SACgB;AAChB,QAAO,mBAAmB,oBAAoB,CAAC,QAAQ,CAAC;;AAG1D,MAAM,qBAAqB,YAAuD;CAChF,QAAQ;CACR,WAAW;CACX,UAAU,aAA0D;EAClE,IAAI,eAAe,SAAS,KAAI,MAAK,EAAE,OAAO,CAAC,QAAO,MAAK,CAAC,CAAC,EAAE;EAC/D,IAAI,eAAe,SAAS,KAAI,MAAK,EAAE,OAAO,CAAC,QAAO,MAAK,CAAC,CAAC,EAAE;EAC/D,IAAI,eAAe,SAAS,KAAI,MAAK,EAAE,OAAO,CAAC,QAAO,MAAK,CAAC,CAAC,EAAE;EAE/D,IAAIA,eAA4B,YAAY;AAC1C,QAAK,IAAI,MAAM,aACb,IAAG,QAAQ;;EAIf,IAAIC,oBAAkC;AACpC,QAAK,IAAI,MAAM,aACb,KAAI;;EAIR,IAAIC,eAAiC,YAAqB;AACxD,QAAK,IAAI,MAAM,aACb,KAAI,GAAG,QAAQ,KAAK,MAAO,QAAO;AAEpC,UAAO;;AAGT,MAAI,aAAa,WAAW,EAE1B,QAAO,EAAE;AAGX,SAAO,0BAA0B;GAC/B,QAAQ;GACR,QAAQ;GACR,QAAQ;GACT,CAAC;;CAEL,CAAC;;;;;;;;;;;;;;;;;ACtCF,SAAgB,oBACd,SACwB;AACxB,QAAO,2BAA2B,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { ProseKitError, defineFacet, defineFacetPayload, editorEventFacet } from "@prosekit/core";
|
|
2
|
+
|
|
3
|
+
//#region src/file/helpers.ts
|
|
4
|
+
function handleFile(view, event, file, handlers) {
|
|
5
|
+
for (let i = handlers.length - 1; i >= 0; i--) {
|
|
6
|
+
const handler = handlers[i];
|
|
7
|
+
if (handler({
|
|
8
|
+
view,
|
|
9
|
+
event,
|
|
10
|
+
file
|
|
11
|
+
})) return true;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
function handleEvent(view, event, handlers, getFiles$2) {
|
|
16
|
+
const files = getFiles$2(event);
|
|
17
|
+
let handled = false;
|
|
18
|
+
for (const file of files) if (handleFile(view, event, file, handlers)) handled = true;
|
|
19
|
+
return handled;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/file/file-drop-handler.ts
|
|
24
|
+
function defineFileDropHandler(handler) {
|
|
25
|
+
return defineFacetPayload(facet$1, [handler]);
|
|
26
|
+
}
|
|
27
|
+
function getFiles$1(event) {
|
|
28
|
+
return Array.from(event.dataTransfer?.files ?? []);
|
|
29
|
+
}
|
|
30
|
+
const facet$1 = defineFacet({
|
|
31
|
+
parent: editorEventFacet,
|
|
32
|
+
singleton: true,
|
|
33
|
+
reducer: (handlers) => {
|
|
34
|
+
const dropHandler = (view, event) => {
|
|
35
|
+
const position = view.posAtCoords({
|
|
36
|
+
left: event.x,
|
|
37
|
+
top: event.y
|
|
38
|
+
});
|
|
39
|
+
if (!position) return false;
|
|
40
|
+
const pos = position.inside > 0 ? position.inside : position.pos;
|
|
41
|
+
return handleEvent(view, event, handlers.map((handler) => (options) => handler({
|
|
42
|
+
...options,
|
|
43
|
+
pos
|
|
44
|
+
})), getFiles$1);
|
|
45
|
+
};
|
|
46
|
+
return ["drop", dropHandler];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/file/file-paste-handler.ts
|
|
52
|
+
function defineFilePasteHandler(handler) {
|
|
53
|
+
return defineFacetPayload(facet, [handler]);
|
|
54
|
+
}
|
|
55
|
+
function getFiles(event) {
|
|
56
|
+
return Array.from(event.clipboardData?.files ?? []);
|
|
57
|
+
}
|
|
58
|
+
const facet = defineFacet({
|
|
59
|
+
parent: editorEventFacet,
|
|
60
|
+
singleton: true,
|
|
61
|
+
reducer: (handlers) => {
|
|
62
|
+
const pasteHandler = (view, event) => {
|
|
63
|
+
return handleEvent(view, event, handlers, getFiles);
|
|
64
|
+
};
|
|
65
|
+
return ["paste", pasteHandler];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/file/file-upload.ts
|
|
71
|
+
/**
|
|
72
|
+
* A class that represents a upload task.
|
|
73
|
+
*/
|
|
74
|
+
var UploadTask = class {
|
|
75
|
+
/**
|
|
76
|
+
* Creates a new upload task. You can find the upload task by its object URL
|
|
77
|
+
* later using `UploadTask.get()`.
|
|
78
|
+
*
|
|
79
|
+
* @param options - The options for the upload task.
|
|
80
|
+
*/
|
|
81
|
+
constructor({ file, uploader }) {
|
|
82
|
+
this.done = false;
|
|
83
|
+
this.subscribers = [];
|
|
84
|
+
this.objectURL = URL.createObjectURL(file);
|
|
85
|
+
this.finished = new Promise((resolve, reject) => {
|
|
86
|
+
const maybePromise = uploader({
|
|
87
|
+
file,
|
|
88
|
+
onProgress: (progress) => {
|
|
89
|
+
if (typeof progress.total !== "number") throw new TypeError("total must be a number");
|
|
90
|
+
if (typeof progress.loaded !== "number") throw new TypeError("loaded must be a number");
|
|
91
|
+
if (progress.loaded > 0) for (const subscriber of this.subscribers) subscriber(progress);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
Promise.resolve(maybePromise).then((result) => {
|
|
95
|
+
this.done = true;
|
|
96
|
+
URL.revokeObjectURL(this.objectURL);
|
|
97
|
+
this.result = result;
|
|
98
|
+
resolve(result);
|
|
99
|
+
}, (err) => {
|
|
100
|
+
this.done = true;
|
|
101
|
+
const error = new ProseKitError("[prosekit] Failed to upload file", { cause: err });
|
|
102
|
+
this.error = error;
|
|
103
|
+
reject(error);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
store.set(this.objectURL, this);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Subscribes to progress updates. Returns a function to unsubscribe.
|
|
110
|
+
*/
|
|
111
|
+
subscribeProgress(callback) {
|
|
112
|
+
this.subscribers.push(callback);
|
|
113
|
+
return () => {
|
|
114
|
+
this.subscribers = this.subscribers.filter((subscriber) => subscriber !== callback);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Finds an upload task from the global store by its object URL.
|
|
119
|
+
*/
|
|
120
|
+
static get(objectURL) {
|
|
121
|
+
return store.get(objectURL);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Deletes an upload task from the global store by its object URL.
|
|
125
|
+
*/
|
|
126
|
+
static delete(objectURL) {
|
|
127
|
+
store.delete(objectURL);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const store = /* @__PURE__ */ new Map();
|
|
131
|
+
|
|
132
|
+
//#endregion
|
|
133
|
+
export { UploadTask, defineFileDropHandler, defineFilePasteHandler };
|
|
134
|
+
//# sourceMappingURL=file-DVUhe5KJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-DVUhe5KJ.js","names":["getFiles","facet","getFiles","dropHandler: DropHandler","pasteHandler: PasteHandler"],"sources":["../src/file/helpers.ts","../src/file/file-drop-handler.ts","../src/file/file-paste-handler.ts","../src/file/file-upload.ts"],"sourcesContent":["import type { EditorView } from '@prosekit/pm/view'\n\ntype FileHandler<E extends Event> = (options: {\n view: EditorView\n event: E\n file: File\n}) => boolean | void\n\nfunction handleFile<E extends Event>(\n view: EditorView,\n event: E,\n file: File,\n handlers: FileHandler<E>[],\n) {\n // The last item in `handlers` should has the highest priority.\n for (let i = handlers.length - 1; i >= 0; i--) {\n const handler = handlers[i]\n if (handler({ view, event, file })) {\n return true\n }\n }\n return false\n}\n\nexport function handleEvent<E extends Event>(\n view: EditorView,\n event: E,\n handlers: FileHandler<E>[],\n getFiles: (event: E) => File[],\n): boolean {\n const files = getFiles(event)\n let handled = false\n for (const file of files) {\n if (handleFile(view, event, file, handlers)) {\n handled = true\n }\n }\n return handled\n}\n","import {\n defineFacet,\n defineFacetPayload,\n editorEventFacet,\n type DropHandler,\n type EditorEventPayload,\n type PlainExtension,\n} from '@prosekit/core'\nimport type { EditorView } from '@prosekit/pm/view'\n\nimport { handleEvent } from './helpers'\n\nexport interface FileDropHandlerOptions {\n /**\n * The editor view.\n */\n view: EditorView\n\n /**\n * The event that triggered the drop.\n */\n event: DragEvent\n\n /**\n * The file that was dropped.\n */\n file: File\n\n /**\n * The position of the document where the file was dropped.\n */\n pos: number\n}\n\n/**\n * A function that handles one of the files in a drop event.\n *\n * Returns `true` if the file was handled and thus should not be handled by\n * other handlers.\n */\nexport type FileDropHandler = (\n options: FileDropHandlerOptions,\n) => boolean | void\n\nexport function defineFileDropHandler(\n handler: FileDropHandler,\n): PlainExtension {\n return defineFacetPayload(facet, [handler]) as PlainExtension\n}\n\nfunction getFiles(event: DragEvent) {\n return Array.from(event.dataTransfer?.files ?? [])\n}\n\nconst facet = defineFacet<FileDropHandler, EditorEventPayload>({\n parent: editorEventFacet,\n singleton: true,\n reducer: (handlers: FileDropHandler[]): EditorEventPayload => {\n const dropHandler: DropHandler = (view, event): boolean => {\n const position = view.posAtCoords({ left: event.x, top: event.y })\n if (!position) {\n return false\n }\n const pos = position.inside > 0 ? position.inside : position.pos\n\n return handleEvent<DragEvent>(\n view,\n event,\n handlers.map((handler) => (options) => handler({ ...options, pos })),\n getFiles,\n )\n }\n return ['drop', dropHandler]\n },\n})\n","import {\n defineFacet,\n defineFacetPayload,\n editorEventFacet,\n type EditorEventPayload,\n type PasteHandler,\n type PlainExtension,\n} from '@prosekit/core'\nimport type { EditorView } from '@prosekit/pm/view'\n\nimport { handleEvent } from './helpers'\n\nexport interface FilePasteHandlerOptions {\n /**\n * The editor view.\n */\n view: EditorView\n\n /**\n * The event that triggered the paste.\n */\n event: ClipboardEvent\n\n /**\n * The file that was pasted.\n */\n file: File\n}\n\n/**\n * A function that handles one of the files in a paste event.\n *\n * Returns `true` if the file was handled and thus should not be handled by\n * other handlers.\n */\nexport type FilePasteHandler = (\n options: FilePasteHandlerOptions,\n) => boolean | void\n\nexport function defineFilePasteHandler(\n handler: FilePasteHandler,\n): PlainExtension {\n return defineFacetPayload(facet, [handler]) as PlainExtension\n}\n\nfunction getFiles(event: ClipboardEvent) {\n return Array.from(event.clipboardData?.files ?? [])\n}\n\nconst facet = defineFacet<FilePasteHandler, EditorEventPayload>({\n parent: editorEventFacet,\n singleton: true,\n reducer: (handlers: FilePasteHandler[]): EditorEventPayload => {\n const pasteHandler: PasteHandler = (view, event): boolean => {\n return handleEvent<ClipboardEvent>(view, event, handlers, getFiles)\n }\n return ['paste', pasteHandler]\n },\n})\n","import { ProseKitError } from '@prosekit/core'\n\n/**\n * An interface representing the upload progress.\n */\nexport interface UploadProgress {\n // A number representing the amount of work already performed by the\n // underlying process.\n loaded: number\n // A number representing the total amount of work that the underlying\n // process is in the progress of performing.\n total: number\n}\n\nexport interface UploaderOptions {\n /**\n * The file to be uploaded.\n */\n file: File\n\n /**\n * A callback function that should be called with the upload progress updates.\n */\n onProgress: (progress: UploadProgress) => void\n}\n\n/**\n * The implementation of the actual upload function. You need to implement this\n * function to upload files to your desired destination.\n */\nexport type Uploader<Result> = (options: UploaderOptions) => Promise<Result>\n\n/**\n * A class that represents a upload task.\n */\nexport class UploadTask<Result> {\n /**\n * An [object URL](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL)\n * representing the file to be uploaded. This URL will be revoked once the\n * upload is complete successfully.\n */\n readonly objectURL: string\n\n /**\n * A boolean indicating whether the upload is complete (either successfully or with an error).\n */\n protected done = false\n\n /**\n * If the upload is complete successfully, this will be the result of the upload.\n */\n protected result: Result | undefined\n\n /**\n * If the upload is complete with an error, this will be the error that occurred.\n */\n protected error: Error | undefined\n\n /**\n * A promise that fulfills once the upload is complete, or rejects if an error occurs.\n */\n readonly finished: Promise<Result>\n\n private subscribers: ((progress: UploadProgress) => void)[] = []\n\n /**\n * Creates a new upload task. You can find the upload task by its object URL\n * later using `UploadTask.get()`.\n *\n * @param options - The options for the upload task.\n */\n constructor({ file, uploader }: { file: File; uploader: Uploader<Result> }) {\n this.objectURL = URL.createObjectURL(file)\n this.finished = new Promise((resolve, reject) => {\n const maybePromise = uploader({\n file,\n onProgress: (progress) => {\n if (typeof progress.total !== 'number') {\n throw new TypeError('total must be a number')\n }\n if (typeof progress.loaded !== 'number') {\n throw new TypeError('loaded must be a number')\n }\n if (progress.loaded > 0) {\n for (const subscriber of this.subscribers) {\n subscriber(progress)\n }\n }\n },\n })\n Promise.resolve(maybePromise).then(\n (result) => {\n this.done = true\n URL.revokeObjectURL(this.objectURL)\n this.result = result\n resolve(result)\n },\n (err) => {\n this.done = true\n const error = new ProseKitError('[prosekit] Failed to upload file', { cause: err })\n this.error = error\n reject(error)\n },\n )\n })\n store.set(this.objectURL, this)\n }\n\n /**\n * Subscribes to progress updates. Returns a function to unsubscribe.\n */\n public subscribeProgress(\n callback: (progress: UploadProgress) => void,\n ): VoidFunction {\n this.subscribers.push(callback)\n return () => {\n this.subscribers = this.subscribers.filter(\n (subscriber) => subscriber !== callback,\n )\n }\n }\n\n /**\n * Finds an upload task from the global store by its object URL.\n */\n static get<Result = unknown>(\n objectURL: string,\n ): UploadTask<Result> | undefined {\n return store.get(objectURL) as UploadTask<Result> | undefined\n }\n\n /**\n * Deletes an upload task from the global store by its object URL.\n */\n static delete(objectURL: string): void {\n store.delete(objectURL)\n }\n}\n\nconst store = new Map<string, UploadTask<unknown>>()\n"],"mappings":";;;AAQA,SAAS,WACP,MACA,OACA,MACA,UACA;AAEA,MAAK,IAAI,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,aAAa;;CAEjC,CAAC;;;;;;;ACvBF,IAAa,aAAb,MAAgC;;;;;;;CAoC9B,YAAY,EAAE,MAAM,YAAwD;cAzB3D;qBAiB6C,EAAE;AAS9D,OAAK,YAAY,IAAI,gBAAgB,KAAK;AAC1C,OAAK,WAAW,IAAI,SAAS,SAAS,WAAW;GAC/C,MAAM,eAAe,SAAS;IAC5B;IACA,aAAa,aAAa;AACxB,SAAI,OAAO,SAAS,UAAU,SAC5B,OAAM,IAAI,UAAU,yBAAyB;AAE/C,SAAI,OAAO,SAAS,WAAW,SAC7B,OAAM,IAAI,UAAU,0BAA0B;AAEhD,SAAI,SAAS,SAAS,EACpB,MAAK,MAAM,cAAc,KAAK,YAC5B,YAAW,SAAS;;IAI3B,CAAC;AACF,WAAQ,QAAQ,aAAa,CAAC,MAC3B,WAAW;AACV,SAAK,OAAO;AACZ,QAAI,gBAAgB,KAAK,UAAU;AACnC,SAAK,SAAS;AACd,YAAQ,OAAO;OAEhB,QAAQ;AACP,SAAK,OAAO;IACZ,MAAM,QAAQ,IAAI,cAAc,oCAAoC,EAAE,OAAO,KAAK,CAAC;AACnF,SAAK,QAAQ;AACb,WAAO,MAAM;KAEhB;IACD;AACF,QAAM,IAAI,KAAK,WAAW,KAAK;;;;;CAMjC,AAAO,kBACL,UACc;AACd,OAAK,YAAY,KAAK,SAAS;AAC/B,eAAa;AACX,QAAK,cAAc,KAAK,YAAY,QACjC,eAAe,eAAe,SAChC;;;;;;CAOL,OAAO,IACL,WACgC;AAChC,SAAO,MAAM,IAAI,UAAU;;;;;CAM7B,OAAO,OAAO,WAAyB;AACrC,QAAM,OAAO,UAAU;;;AAI3B,MAAM,wBAAQ,IAAI,KAAkC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { PlainExtension } from "@prosekit/core";
|
|
2
|
+
import { EditorView } from "@prosekit/pm/view";
|
|
3
|
+
|
|
4
|
+
//#region src/file/file-drop-handler.d.ts
|
|
5
|
+
interface FileDropHandlerOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The editor view.
|
|
8
|
+
*/
|
|
9
|
+
view: EditorView;
|
|
10
|
+
/**
|
|
11
|
+
* The event that triggered the drop.
|
|
12
|
+
*/
|
|
13
|
+
event: DragEvent;
|
|
14
|
+
/**
|
|
15
|
+
* The file that was dropped.
|
|
16
|
+
*/
|
|
17
|
+
file: File;
|
|
18
|
+
/**
|
|
19
|
+
* The position of the document where the file was dropped.
|
|
20
|
+
*/
|
|
21
|
+
pos: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A function that handles one of the files in a drop event.
|
|
25
|
+
*
|
|
26
|
+
* Returns `true` if the file was handled and thus should not be handled by
|
|
27
|
+
* other handlers.
|
|
28
|
+
*/
|
|
29
|
+
type FileDropHandler = (options: FileDropHandlerOptions) => boolean | void;
|
|
30
|
+
declare function defineFileDropHandler(handler: FileDropHandler): PlainExtension;
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/file/file-paste-handler.d.ts
|
|
33
|
+
interface FilePasteHandlerOptions {
|
|
34
|
+
/**
|
|
35
|
+
* The editor view.
|
|
36
|
+
*/
|
|
37
|
+
view: EditorView;
|
|
38
|
+
/**
|
|
39
|
+
* The event that triggered the paste.
|
|
40
|
+
*/
|
|
41
|
+
event: ClipboardEvent;
|
|
42
|
+
/**
|
|
43
|
+
* The file that was pasted.
|
|
44
|
+
*/
|
|
45
|
+
file: File;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A function that handles one of the files in a paste event.
|
|
49
|
+
*
|
|
50
|
+
* Returns `true` if the file was handled and thus should not be handled by
|
|
51
|
+
* other handlers.
|
|
52
|
+
*/
|
|
53
|
+
type FilePasteHandler = (options: FilePasteHandlerOptions) => boolean | void;
|
|
54
|
+
declare function defineFilePasteHandler(handler: FilePasteHandler): PlainExtension;
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/file/file-upload.d.ts
|
|
57
|
+
/**
|
|
58
|
+
* An interface representing the upload progress.
|
|
59
|
+
*/
|
|
60
|
+
interface UploadProgress {
|
|
61
|
+
loaded: number;
|
|
62
|
+
total: number;
|
|
63
|
+
}
|
|
64
|
+
interface UploaderOptions {
|
|
65
|
+
/**
|
|
66
|
+
* The file to be uploaded.
|
|
67
|
+
*/
|
|
68
|
+
file: File;
|
|
69
|
+
/**
|
|
70
|
+
* A callback function that should be called with the upload progress updates.
|
|
71
|
+
*/
|
|
72
|
+
onProgress: (progress: UploadProgress) => void;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The implementation of the actual upload function. You need to implement this
|
|
76
|
+
* function to upload files to your desired destination.
|
|
77
|
+
*/
|
|
78
|
+
type Uploader<Result> = (options: UploaderOptions) => Promise<Result>;
|
|
79
|
+
/**
|
|
80
|
+
* A class that represents a upload task.
|
|
81
|
+
*/
|
|
82
|
+
declare class UploadTask<Result> {
|
|
83
|
+
/**
|
|
84
|
+
* An [object URL](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL)
|
|
85
|
+
* representing the file to be uploaded. This URL will be revoked once the
|
|
86
|
+
* upload is complete successfully.
|
|
87
|
+
*/
|
|
88
|
+
readonly objectURL: string;
|
|
89
|
+
/**
|
|
90
|
+
* A boolean indicating whether the upload is complete (either successfully or with an error).
|
|
91
|
+
*/
|
|
92
|
+
protected done: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* If the upload is complete successfully, this will be the result of the upload.
|
|
95
|
+
*/
|
|
96
|
+
protected result: Result | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* If the upload is complete with an error, this will be the error that occurred.
|
|
99
|
+
*/
|
|
100
|
+
protected error: Error | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* A promise that fulfills once the upload is complete, or rejects if an error occurs.
|
|
103
|
+
*/
|
|
104
|
+
readonly finished: Promise<Result>;
|
|
105
|
+
private subscribers;
|
|
106
|
+
/**
|
|
107
|
+
* Creates a new upload task. You can find the upload task by its object URL
|
|
108
|
+
* later using `UploadTask.get()`.
|
|
109
|
+
*
|
|
110
|
+
* @param options - The options for the upload task.
|
|
111
|
+
*/
|
|
112
|
+
constructor({
|
|
113
|
+
file,
|
|
114
|
+
uploader
|
|
115
|
+
}: {
|
|
116
|
+
file: File;
|
|
117
|
+
uploader: Uploader<Result>;
|
|
118
|
+
});
|
|
119
|
+
/**
|
|
120
|
+
* Subscribes to progress updates. Returns a function to unsubscribe.
|
|
121
|
+
*/
|
|
122
|
+
subscribeProgress(callback: (progress: UploadProgress) => void): VoidFunction;
|
|
123
|
+
/**
|
|
124
|
+
* Finds an upload task from the global store by its object URL.
|
|
125
|
+
*/
|
|
126
|
+
static get<Result = unknown>(objectURL: string): UploadTask<Result> | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* Deletes an upload task from the global store by its object URL.
|
|
129
|
+
*/
|
|
130
|
+
static delete(objectURL: string): void;
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
export { type FileDropHandler, type FileDropHandlerOptions, type FilePasteHandler, type FilePasteHandlerOptions, type UploadProgress, UploadTask, type Uploader, type UploaderOptions, defineFileDropHandler, defineFilePasteHandler };
|
|
134
|
+
//# sourceMappingURL=index-DY6lIIYV.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-DY6lIIYV.d.ts","names":[],"sources":["../src/file/file-drop-handler.ts","../src/file/file-paste-handler.ts","../src/file/file-upload.ts"],"sourcesContent":[],"mappings":";;;;UAYiB,sBAAA;;AAAjB;;MAIQ,EAAA,UAAA;;;;EAwBI,KAAA,EAnBH,SAmBG;EAII;;;MAEb,EApBK,IAoBL;EAAc;;;;AClCjB;;;;;;AAuBA;AAIgB,KDCJ,eAAA,GCD0B,CAAA,OAAA,EDE3B,sBCF2B,EAAA,GAAA,OAAA,GAAA,IAAA;AAAA,iBDKtB,qBAAA,CCLsB,OAAA,EDM3B,eCN2B,CAAA,EDOnC,cCPmC;;;UA3BrB,uBAAA;;ADAjB;;MAIQ,ECAA,UDAA;;;;EAwBI,KAAA,ECnBH,cDmBkB;EAIX;;;MAEb,ECpBK,IDoBL;;;;;AClCH;;;AASS,KAcG,gBAAA,GAdH,CAAA,OAAA,EAeE,uBAfF,EAAA,GAAA,OAAA,GAAA,IAAA;AAKD,iBAaQ,sBAAA,CAbR,OAAA,EAcG,gBAdH,CAAA,EAeL,cAfK;;;;;;UCrBS,cAAA;EFOA,MAAA,EAAA,MAAA;EAAsB,KAAA,EAAA,MAAA;;AAS9B,UEPQ,eAAA,CFOR;;;AAmBT;EAIgB,IAAA,EE1BR,IF0BQ;EAAqB;;;EAEpB,UAAA,EAAA,CAAA,QAAA,EEvBQ,cFuBR,EAAA,GAAA,IAAA;;;;AClCjB;;AAIQ,KCcI,QDdJ,CAAA,MAAA,CAAA,GAAA,CAAA,OAAA,ECciC,eDdjC,EAAA,GCcqD,ODdrD,CCc6D,MDd7D,CAAA;;;;AAmBI,cCAC,UDAe,CAAA,MACjB,CAAA,CAAA;EAGK;;;;;;;;AClChB;EASiB,UAAA,IAAA,EAAA,OAAe;EAAA;;;EASO,UAAA,MAAA,EA4BnB,MA5BmB,GAAA,SAAA;EAO3B;;;YAAyD,KAAA,EA0BlD,KA1BkD,GAAA,SAAA;;;AAKrE;EAAuB,SAAA,QAAA,EA0BF,OA1BE,CA0BM,MA1BN,CAAA;UAgBH,WAAA;;;;;;;aAoB+C,CAAA;IAAA,IAAA;IAAA;GAAA,EAAA;IAAT,IAAA,EAAhB,IAAgB;IAyCjC,QAAA,EAzCiC,QAyCjC,CAzC0C,MAyC1C,CAAA;;;;;yCAAA,0BACpB;;;;mDAcA,WAAW"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mark-rule-BCqIZMDu.js","names":["stack: Array<[start: number, node: ProseMirrorNode]>","ranges: Array<[number, number]>","start","ranges: MarkRange[]","result: MarkRange[]","mark","tr","toRemove: MarkRange[]","toCreate: MarkRange[]","rules: MarkRuleOptions[]"],"sources":["../src/mark-rule/range.ts","../src/mark-rule/apply.ts","../src/mark-rule/mark-rule.ts"],"sourcesContent":["import type {\n ProseMirrorNode,\n ResolvedPos,\n} from '@prosekit/pm/model'\nimport type {\n EditorState,\n Transaction,\n} from '@prosekit/pm/state'\nimport type { ProsemirrorNode } from 'prosemirror-flat-list'\n\nfunction getSpanTextRanges($from: ResolvedPos, $to: ResolvedPos) {\n const nodeRange = $from.blockRange($to)\n if (!nodeRange) {\n return []\n }\n\n const stack: Array<[start: number, node: ProseMirrorNode]> = []\n let start = nodeRange.start\n\n for (let i = nodeRange.startIndex; i < nodeRange.endIndex; i++) {\n const child = nodeRange.parent.child(i)\n stack.push([start, child])\n start += child.nodeSize\n }\n\n const ranges: Array<[number, number]> = []\n\n while (stack.length > 0) {\n const [start, node] = stack.pop()!\n if (node.type.spec.code) {\n continue\n }\n\n if (node.type.isTextblock) {\n ranges.push([start + 1, start + 1 + node.content.size])\n continue\n }\n\n node.forEach((child, offset) => {\n stack.push([start + offset + 1, child])\n })\n }\n\n return ranges\n}\n\nfunction getInlineTextRange(\n $from: ResolvedPos,\n $to: ResolvedPos,\n): [number, number] {\n return [$from.start(), $to.end()]\n}\n\nfunction getTextRanges(\n doc: ProsemirrorNode,\n from: number,\n to: number,\n): Array<[number, number]> {\n const $from = doc.resolve(from)\n const $to = doc.resolve(to)\n\n if ($from.sameParent($to) && $from.parent.isTextblock) {\n return [getInlineTextRange($from, $to)]\n } else {\n const nodeRange = $from.blockRange($to)\n if (!nodeRange) {\n return []\n }\n\n return getSpanTextRanges($from, $to)\n }\n}\n\nfunction getMapRange(\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n) {\n let lo = oldState.selection.from\n let hi = oldState.selection.to\n\n for (const tr of transactions) {\n for (const map of tr.mapping.maps) {\n lo = map.map(lo)\n hi = map.map(hi)\n\n map.forEach((_oldStart, _oldEnd, newStart, newEnd) => {\n lo = Math.min(lo, hi, newStart)\n hi = Math.max(lo, hi, newEnd)\n })\n }\n }\n\n lo = Math.min(lo, hi, newState.selection.from)\n hi = Math.min(lo, hi, newState.selection.to)\n\n return [lo, hi] as const\n}\n\nexport function getCheckRanges(\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n): Array<[number, number]> {\n const [from, to] = getMapRange(transactions, oldState, newState)\n return getTextRanges(newState.doc, from, to)\n}\n","import {\n getMarkType,\n maybeRun,\n OBJECT_REPLACEMENT_CHARACTER,\n} from '@prosekit/core'\nimport type {\n Mark,\n ProseMirrorNode,\n} from '@prosekit/pm/model'\nimport type {\n EditorState,\n Transaction,\n} from '@prosekit/pm/state'\n\nimport { getCheckRanges } from './range'\nimport type { MarkRuleOptions } from './types'\n\ntype MarkRange = [from: number, to: number, mark: Mark]\n\nfunction getExpectedMarkings(\n rules: MarkRuleOptions[],\n doc: ProseMirrorNode,\n from: number,\n to: number,\n): MarkRange[] {\n const text = doc.textBetween(from, to, null, OBJECT_REPLACEMENT_CHARACTER)\n const ranges: MarkRange[] = []\n\n for (const rule of rules) {\n rule.regex.lastIndex = 0\n const matches = text.matchAll(rule.regex)\n const markType = getMarkType(doc.type.schema, rule.type)\n\n for (const match of matches) {\n const index = match.index\n if (index == null) continue\n const attrs = maybeRun(rule.attrs, match)\n const mark = markType.create(attrs)\n ranges.push([from + index, from + index + match[0].length, mark])\n }\n }\n\n // Sort by start position. If start positions are equal, the longer match\n // should be prioritized.\n ranges.sort((a, b) => a[0] - b[0] || b[1] - a[1])\n\n // Remove overlapped marks.\n const result: MarkRange[] = []\n let freeIndex = 0\n\n for (const range of ranges) {\n if (range[0] >= freeIndex) {\n result.push(range)\n freeIndex = range[1]\n }\n }\n\n return result\n}\n\nfunction getReceivedMarkings(\n rules: MarkRuleOptions[],\n doc: ProseMirrorNode,\n from: number,\n to: number,\n): MarkRange[] {\n const result: MarkRange[] = []\n const schema = doc.type.schema\n const markTypes = rules.map((rule) => getMarkType(schema, rule.type))\n\n doc.nodesBetween(from, to, (node, pos) => {\n if (!node.isInline) {\n return\n }\n\n for (const markType of markTypes) {\n const mark = node.marks.find((mark) => mark.type === markType)\n if (mark) {\n result.push([pos, pos + node.nodeSize, mark])\n }\n }\n })\n return result\n}\n\nfunction markRangeEquals(a: MarkRange, b: MarkRange): boolean {\n return a[0] === b[0] && a[1] === b[1] && a[2].eq(b[2])\n}\n\nfunction markRangeDiffs(a: MarkRange[], b: MarkRange[]): MarkRange[] {\n return a.filter((x) => !b.some((y) => markRangeEquals(x, y)))\n}\n\nexport function applyMarkRules(\n rules: MarkRuleOptions[],\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n): Transaction | null {\n if (transactions.length === 0 || transactions.every((tr) => !tr.docChanged)) {\n return null\n }\n\n const ranges = getCheckRanges(transactions, oldState, newState)\n\n const toRemove: MarkRange[] = []\n const toCreate: MarkRange[] = []\n\n for (const [from, to] of ranges) {\n const expected = getExpectedMarkings(rules, newState.doc, from, to)\n const received = getReceivedMarkings(rules, newState.doc, from, to)\n\n toRemove.push(...markRangeDiffs(received, expected))\n toCreate.push(...markRangeDiffs(expected, received))\n }\n\n if (toCreate.length === 0 && toRemove.length === 0) {\n return null\n }\n\n const tr = newState.tr\n for (const [from, to, mark] of toRemove) {\n tr.removeMark(from, to, mark)\n }\n for (const [from, to, mark] of toCreate) {\n tr.addMark(from, to, mark)\n }\n return tr\n}\n","import {\n defineFacet,\n defineFacetPayload,\n pluginFacet,\n type PlainExtension,\n type PluginPayload,\n} from '@prosekit/core'\nimport {\n PluginKey,\n ProseMirrorPlugin,\n type EditorState,\n type Transaction,\n} from '@prosekit/pm/state'\n\nimport { applyMarkRules } from './apply'\nimport type { MarkRuleOptions } from './types'\n\n/**\n * A mark rule is something that can automatically apply marks to text if it\n * matches a certain pattern, and remove them if it doesn't match anymore.\n */\nexport function defineMarkRule(options: MarkRuleOptions): PlainExtension {\n return defineFacetPayload(markRuleFacet, [options]) as PlainExtension\n}\n\nconst markRuleFacet = defineFacet<MarkRuleOptions, PluginPayload>({\n reduce: () => {\n let rules: MarkRuleOptions[] = []\n\n const plugin = new ProseMirrorPlugin({\n key: new PluginKey('prosekit-mark-rule'),\n appendTransaction: (\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n ) => {\n return applyMarkRules(rules, transactions, oldState, newState)\n },\n })\n\n return function reducer(input) {\n rules = input\n return plugin\n }\n },\n\n parent: pluginFacet,\n})\n"],"mappings":";;;;AAUA,SAAS,kBAAkB,OAAoB,KAAkB;CAC/D,MAAM,YAAY,MAAM,WAAW,IAAI;AACvC,KAAI,CAAC,UACH,QAAO,EAAE;CAGX,MAAMA,QAAuD,EAAE;CAC/D,IAAI,QAAQ,UAAU;AAEtB,MAAK,IAAI,IAAI,UAAU,YAAY,IAAI,UAAU,UAAU,KAAK;EAC9D,MAAM,QAAQ,UAAU,OAAO,MAAM,EAAE;AACvC,QAAM,KAAK,CAAC,OAAO,MAAM,CAAC;AAC1B,WAAS,MAAM;;CAGjB,MAAMC,SAAkC,EAAE;AAE1C,QAAO,MAAM,SAAS,GAAG;EACvB,MAAM,CAACC,SAAO,QAAQ,MAAM,KAAK;AACjC,MAAI,KAAK,KAAK,KAAK,KACjB;AAGF,MAAI,KAAK,KAAK,aAAa;AACzB,UAAO,KAAK,CAACA,UAAQ,GAAGA,UAAQ,IAAI,KAAK,QAAQ,KAAK,CAAC;AACvD;;AAGF,OAAK,SAAS,OAAO,WAAW;AAC9B,SAAM,KAAK,CAACA,UAAQ,SAAS,GAAG,MAAM,CAAC;IACvC;;AAGJ,QAAO;;AAGT,SAAS,mBACP,OACA,KACkB;AAClB,QAAO,CAAC,MAAM,OAAO,EAAE,IAAI,KAAK,CAAC;;AAGnC,SAAS,cACP,KACA,MACA,IACyB;CACzB,MAAM,QAAQ,IAAI,QAAQ,KAAK;CAC/B,MAAM,MAAM,IAAI,QAAQ,GAAG;AAE3B,KAAI,MAAM,WAAW,IAAI,IAAI,MAAM,OAAO,YACxC,QAAO,CAAC,mBAAmB,OAAO,IAAI,CAAC;MAClC;AAEL,M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AM,WAAW,oBAAoB,OAAO,SAAS,KAAK,MAAM,GAAG;AAEnE,WAAS,KAAK,GAAG,eAAe,UAAU,SAAS,CAAC;AACpD,WAAS,KAAK,GAAG,eAAe,UAAU,SAAS,CAAC;;AAGtD,KAAI,SAAS,WAAW,KAAK,SAAS,WAAW,EAC/C,QAAO;CAGT,MAAM,KAAK,SAAS;AACpB,MAAK,MAAM,CAAC,MAAM,IAAI,SAAS,SAC7B,IAAG,WAAW,MAAM,IAAI,KAAK;AAE/B,MAAK,MAAM,CAAC,MAAM,IAAI,SAAS,SAC7B,IAAG,QAAQ,MAAM,IAAI,KAAK;AAE5B,QAAO;;;;;;;;;AC1GT,SAAgB,eAAe,SAA0C;AACvE,QAAO,mBAAmB,eAAe,CAAC,QAAQ,CAAC;;AAGrD,MAAM,gBAAgB,YAA4C;CAChE,cAAc;EACZ,IAAIC,QAA2B,EAAE;EAEjC,MAAM,SAAS,IAAI,kBAAkB;GACnC,KAAK,IAAI,UAAU,qBAAqB;GACxC,oBACE,cACA,UACA,aACG;AACH,WAAO,eAAe,OAAO,cAAc,UAAU,SAAS;;GAEjE,CAAC;AAEF,SAAO,SAAS,QAAQ,OAAO;AAC7B,WAAQ;AACR,UAAO;;;CAIX,QAAQ;CACT,CAAC"}
|
|
1
|
+
{"version":3,"file":"mark-rule-CGmswjQ_.js","names":["stack: Array<[start: number, node: ProseMirrorNode]>","ranges: Array<[number, number]>","start","ranges: MarkRange[]","result: MarkRange[]","mark","tr","toRemove: MarkRange[]","toCreate: MarkRange[]","rules: MarkRuleOptions[]"],"sources":["../src/mark-rule/range.ts","../src/mark-rule/apply.ts","../src/mark-rule/mark-rule.ts"],"sourcesContent":["import type {\n ProseMirrorNode,\n ResolvedPos,\n} from '@prosekit/pm/model'\nimport type {\n EditorState,\n Transaction,\n} from '@prosekit/pm/state'\nimport type { ProsemirrorNode } from 'prosemirror-flat-list'\n\nfunction getSpanTextRanges($from: ResolvedPos, $to: ResolvedPos) {\n const nodeRange = $from.blockRange($to)\n if (!nodeRange) {\n return []\n }\n\n const stack: Array<[start: number, node: ProseMirrorNode]> = []\n let start = nodeRange.start\n\n for (let i = nodeRange.startIndex; i < nodeRange.endIndex; i++) {\n const child = nodeRange.parent.child(i)\n stack.push([start, child])\n start += child.nodeSize\n }\n\n const ranges: Array<[number, number]> = []\n\n while (stack.length > 0) {\n const [start, node] = stack.pop()!\n if (node.type.spec.code) {\n continue\n }\n\n if (node.type.isTextblock) {\n ranges.push([start + 1, start + 1 + node.content.size])\n continue\n }\n\n node.forEach((child, offset) => {\n stack.push([start + offset + 1, child])\n })\n }\n\n return ranges\n}\n\nfunction getInlineTextRange(\n $from: ResolvedPos,\n $to: ResolvedPos,\n): [number, number] {\n return [$from.start(), $to.end()]\n}\n\nfunction getTextRanges(\n doc: ProsemirrorNode,\n from: number,\n to: number,\n): Array<[number, number]> {\n const $from = doc.resolve(from)\n const $to = doc.resolve(to)\n\n if ($from.sameParent($to) && $from.parent.isTextblock) {\n return [getInlineTextRange($from, $to)]\n } else {\n const nodeRange = $from.blockRange($to)\n if (!nodeRange) {\n return []\n }\n\n return getSpanTextRanges($from, $to)\n }\n}\n\nfunction getMapRange(\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n) {\n let lo = oldState.selection.from\n let hi = oldState.selection.to\n\n for (const tr of transactions) {\n for (const map of tr.mapping.maps) {\n lo = map.map(lo)\n hi = map.map(hi)\n\n map.forEach((_oldStart, _oldEnd, newStart, newEnd) => {\n lo = Math.min(lo, hi, newStart)\n hi = Math.max(lo, hi, newEnd)\n })\n }\n }\n\n lo = Math.min(lo, hi, newState.selection.from)\n hi = Math.min(lo, hi, newState.selection.to)\n\n return [lo, hi] as const\n}\n\nexport function getCheckRanges(\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n): Array<[number, number]> {\n const [from, to] = getMapRange(transactions, oldState, newState)\n return getTextRanges(newState.doc, from, to)\n}\n","import {\n getMarkType,\n maybeRun,\n OBJECT_REPLACEMENT_CHARACTER,\n} from '@prosekit/core'\nimport type {\n Mark,\n ProseMirrorNode,\n} from '@prosekit/pm/model'\nimport type {\n EditorState,\n Transaction,\n} from '@prosekit/pm/state'\n\nimport { getCheckRanges } from './range'\nimport type { MarkRuleOptions } from './types'\n\ntype MarkRange = [from: number, to: number, mark: Mark]\n\nfunction getExpectedMarkings(\n rules: MarkRuleOptions[],\n doc: ProseMirrorNode,\n from: number,\n to: number,\n): MarkRange[] {\n const text = doc.textBetween(from, to, null, OBJECT_REPLACEMENT_CHARACTER)\n const ranges: MarkRange[] = []\n\n for (const rule of rules) {\n rule.regex.lastIndex = 0\n const matches = text.matchAll(rule.regex)\n const markType = getMarkType(doc.type.schema, rule.type)\n\n for (const match of matches) {\n const index = match.index\n if (index == null) continue\n const attrs = maybeRun(rule.attrs, match)\n const mark = markType.create(attrs)\n ranges.push([from + index, from + index + match[0].length, mark])\n }\n }\n\n // Sort by start position. If start positions are equal, the longer match\n // should be prioritized.\n ranges.sort((a, b) => a[0] - b[0] || b[1] - a[1])\n\n // Remove overlapped marks.\n const result: MarkRange[] = []\n let freeIndex = 0\n\n for (const range of ranges) {\n if (range[0] >= freeIndex) {\n result.push(range)\n freeIndex = range[1]\n }\n }\n\n return result\n}\n\nfunction getReceivedMarkings(\n rules: MarkRuleOptions[],\n doc: ProseMirrorNode,\n from: number,\n to: number,\n): MarkRange[] {\n const result: MarkRange[] = []\n const schema = doc.type.schema\n const markTypes = rules.map((rule) => getMarkType(schema, rule.type))\n\n doc.nodesBetween(from, to, (node, pos) => {\n if (!node.isInline) {\n return\n }\n\n for (const markType of markTypes) {\n const mark = node.marks.find((mark) => mark.type === markType)\n if (mark) {\n result.push([pos, pos + node.nodeSize, mark])\n }\n }\n })\n return result\n}\n\nfunction markRangeEquals(a: MarkRange, b: MarkRange): boolean {\n return a[0] === b[0] && a[1] === b[1] && a[2].eq(b[2])\n}\n\nfunction markRangeDiffs(a: MarkRange[], b: MarkRange[]): MarkRange[] {\n return a.filter((x) => !b.some((y) => markRangeEquals(x, y)))\n}\n\nexport function applyMarkRules(\n rules: MarkRuleOptions[],\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n): Transaction | null {\n if (transactions.length === 0 || transactions.every((tr) => !tr.docChanged)) {\n return null\n }\n\n const ranges = getCheckRanges(transactions, oldState, newState)\n\n const toRemove: MarkRange[] = []\n const toCreate: MarkRange[] = []\n\n for (const [from, to] of ranges) {\n const expected = getExpectedMarkings(rules, newState.doc, from, to)\n const received = getReceivedMarkings(rules, newState.doc, from, to)\n\n toRemove.push(...markRangeDiffs(received, expected))\n toCreate.push(...markRangeDiffs(expected, received))\n }\n\n if (toCreate.length === 0 && toRemove.length === 0) {\n return null\n }\n\n const tr = newState.tr\n for (const [from, to, mark] of toRemove) {\n tr.removeMark(from, to, mark)\n }\n for (const [from, to, mark] of toCreate) {\n tr.addMark(from, to, mark)\n }\n return tr\n}\n","import {\n defineFacet,\n defineFacetPayload,\n pluginFacet,\n type PlainExtension,\n type PluginPayload,\n} from '@prosekit/core'\nimport {\n PluginKey,\n ProseMirrorPlugin,\n type EditorState,\n type Transaction,\n} from '@prosekit/pm/state'\n\nimport { applyMarkRules } from './apply'\nimport type { MarkRuleOptions } from './types'\n\n/**\n * A mark rule is something that can automatically apply marks to text if it\n * matches a certain pattern, and remove them if it doesn't match anymore.\n */\nexport function defineMarkRule(options: MarkRuleOptions): PlainExtension {\n return defineFacetPayload(markRuleFacet, [options]) as PlainExtension\n}\n\nconst markRuleFacet = defineFacet<MarkRuleOptions, PluginPayload>({\n reduce: () => {\n let rules: MarkRuleOptions[] = []\n\n const plugin = new ProseMirrorPlugin({\n key: new PluginKey('prosekit-mark-rule'),\n appendTransaction: (\n transactions: readonly Transaction[],\n oldState: EditorState,\n newState: EditorState,\n ) => {\n return applyMarkRules(rules, transactions, oldState, newState)\n },\n })\n\n return function reducer(input) {\n rules = input\n return plugin\n }\n },\n\n parent: pluginFacet,\n})\n"],"mappings":";;;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MAAM,CAAC,MAAM,IAAI,SAAS,SAC7B,IAAG,WAAW,MAAM,IAAI,KAAK;AAE/B,MAAK,MAAM,CAAC,MAAM,IAAI,SAAS,SAC7B,IAAG,QAAQ,MAAM,IAAI,KAAK;AAE5B,QAAO;;;;;;;;;AC1GT,SAAgB,eAAe,SAA0C;AACvE,QAAO,mBAAmB,eAAe,CAAC,QAAQ,CAAC;;AAGrD,MAAM,gBAAgB,YAA4C;CAChE,cAAc;EACZ,IAAIC,QAA2B,EAAE;EAEjC,MAAM,SAAS,IAAI,kBAAkB;GACnC,KAAK,IAAI,UAAU,qBAAqB;GACxC,oBACE,cACA,UACA,aACG;AACH,WAAO,eAAe,OAAO,cAAc,UAAU,SAAS;;GAEjE,CAAC;AAEF,SAAO,SAAS,QAAQ,OAAO;AAC7B,WAAQ;AACR,UAAO;;;CAIX,QAAQ;CACT,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paste-rule-DIEJKIje.js","names":["handlers: PasteRulePayload[]","chunks: Array<[string, RegExpExecArray | undefined]>","match: RegExpExecArray | null","children: ProseMirrorNode[]","newChildren: ProseMirrorNode[]","nodes: ProseMirrorNode[]","text"],"sources":["../src/paste-rule/paste-rule-plugin.ts","../src/paste-rule/paste-rule.ts","../src/paste-rule/split-text-by-regex.ts","../src/paste-rule/mark-paste-rule.ts"],"sourcesContent":["import {\n defineFacet,\n defineFacetPayload,\n pluginFacet,\n type PlainExtension,\n type PluginPayload,\n} from '@prosekit/core'\nimport type { Slice } from '@prosekit/pm/model'\nimport {\n PluginKey,\n ProseMirrorPlugin,\n} from '@prosekit/pm/state'\nimport type { EditorView } from '@prosekit/pm/view'\n\ntype PasteRulePayload = (options: { slice: Slice; view: EditorView; plain: boolean }) => Slice\n\n/**\n * @internal\n */\nconst pasteRuleFacet = defineFacet<PasteRulePayload, PluginPayload>({\n reduce: () => {\n let handlers: PasteRulePayload[] = []\n\n const transformPasted = (slice: Slice, view: EditorView, plain: boolean): Slice => {\n for (const handler of handlers) {\n slice = handler({ slice, view, plain })\n }\n return slice\n }\n\n const plugin = new ProseMirrorPlugin({\n key: new PluginKey('prosekit-paste-rule'),\n props: {\n transformPasted,\n },\n })\n\n return (inputs: PasteRulePayload[]) => {\n // Last added rule (highest priority) is applied first\n handlers = [...inputs].reverse()\n return plugin\n }\n },\n singleton: true,\n parent: pluginFacet,\n})\n\n/**\n * @internal\n */\nexport function definePasteRulePlugin(payload: PasteRulePayload): PlainExtension {\n return defineFacetPayload(pasteRuleFacet, [payload]) as PlainExtension\n}\n","import type { PlainExtension } from '@prosekit/core'\nimport type { Slice } from '@prosekit/pm/model'\nimport type { EditorView } from '@prosekit/pm/view'\n\nimport { definePasteRulePlugin } from './paste-rule-plugin'\n\n/**\n * @public\n *\n * Options for {@link PasteRuleHandler}.\n */\nexport interface PasteRuleHandlerOptions {\n /**\n * The slice to be pasted.\n */\n slice: Slice\n\n /**\n * The editor view.\n */\n view: EditorView\n\n /**\n * Whether the pasted content is treated as plain text. This is true when the\n * `Shift` key is held when pasting.\n */\n plain: boolean\n}\n\n/**\n * @public\n *\n * Can be used to transform pasted or dragged-and-dropped content before it is\n * applied to the document.\n */\nexport type PasteRuleHandler = (options: PasteRuleHandlerOptions) => Slice\n\n/**\n * Options for {@link definePasteRule}.\n *\n * @public\n */\nexport interface PasteRuleOptions {\n /**\n * A function to be called when a paste rule is triggered.\n */\n handler: PasteRuleHandler\n}\n\n/**\n * Defines a paste rule. This rule allows you to modify pasted or dragged\n * content before it is inserted into the document.\n *\n * @param options\n *\n * @public\n */\nexport function definePasteRule({ handler }: PasteRuleOptions): PlainExtension {\n return definePasteRulePlugin(handler)\n}\n","/**\n * Splits text into chunks based on regex matches, preserving both matched and unmatched segments.\n * Returns an array of tuples where each tuple contains a text segment and either the match data\n * (for matched segments) or undefined (for unmatched segments).\n */\nexport function splitTextByRegex(\n text: string,\n regex: RegExp,\n): Array<[string, RegExpExecArray | undefined]> | undefined {\n regex.lastIndex = 0\n\n const chunks: Array<[string, RegExpExecArray | undefined]> = []\n let lastIndex = 0\n let match: RegExpExecArray | null\n let matched = false\n\n while ((match = regex.exec(text))) {\n const start = match.index\n const end = regex.lastIndex\n\n // Push the unmatched prefix, if any.\n if (start > lastIndex) {\n chunks.push([text.slice(lastIndex, start), undefined])\n }\n\n // Push the matched segment.\n chunks.push([text.slice(start, end), match])\n matched = true\n\n if (lastIndex === end) {\n // Safeguard against zero-width matches that would otherwise cause an infinite loop.\n return\n }\n lastIndex = end\n }\n\n if (matched && lastIndex < text.length) {\n chunks.push([text.slice(lastIndex), undefined])\n }\n\n regex.lastIndex = 0\n\n return matched ? chunks : undefined\n}\n","import {\n getMarkType,\n type PlainExtension,\n} from '@prosekit/core'\nimport type {\n Attrs,\n MarkType,\n ProseMirrorNode,\n} from '@prosekit/pm/model'\nimport {\n Fragment,\n Slice,\n} from '@prosekit/pm/model'\n\nimport { definePasteRule } from './paste-rule'\nimport { splitTextByRegex } from './split-text-by-regex'\n\n/**\n * The options for {@link defineMarkPasteRule}.\n *\n * @public\n */\nexport interface MarkPasteRuleOptions {\n /**\n * The regular expression to match against. It must have a `g` flag to match\n * all instances of the mark.\n */\n regex: RegExp\n\n /**\n * The mark type to apply to the matched text.\n */\n type: string | MarkType\n\n /**\n * A function used to compute attributes to set on the mark created by this\n * rule. When it returns `false`, the rule won't match. When it returns `null`\n * or `undefined`, that is interpreted as an empty/default set of attributes.\n * @default null\n */\n getAttrs?: (match: RegExpExecArray) => Attrs | null | undefined | false\n\n /**\n * Optional function to determine if a text node should be skipped.\n * Default behavior: skip code nodes and nodes that already have the target mark.\n */\n shouldSkip?: (node: ProseMirrorNode) => boolean\n}\n\n/**\n * Defines a paste rule that applies marks based on regex patterns.\n *\n * @public\n */\nexport function defineMarkPasteRule(options: MarkPasteRuleOptions): PlainExtension {\n return definePasteRule({\n handler: ({ slice, view, plain }) => {\n if (plain) {\n return slice\n }\n\n const markType = getMarkType(view.state.schema, options.type)\n\n return replaceMarkInSlice({\n markType,\n regex: options.regex,\n getAttrs: options.getAttrs,\n shouldSkip: options.shouldSkip,\n }, slice)\n },\n })\n}\n\ninterface MarkPasteRuleHandlerOptions {\n markType: MarkType\n regex: RegExp\n getAttrs?: (match: RegExpExecArray) => Attrs | null | undefined | false\n shouldSkip?: (node: ProseMirrorNode) => boolean\n}\n\nfunction replaceMarkInSlice(options: MarkPasteRuleHandlerOptions, slice: Slice): Slice {\n const newFragment = replaceMarkInFragment(options, slice.content)\n if (!newFragment) {\n return slice\n }\n return new Slice(newFragment, slice.openStart, slice.openEnd)\n}\n\nfunction replaceMarkInFragment(options: MarkPasteRuleHandlerOptions, fragment: Fragment): Fragment | undefined {\n let changed = false\n let children: ProseMirrorNode[] = []\n\n for (const child of fragment.content) {\n const newChild = replaceMarkInNode(options, child)\n if (newChild) {\n changed = true\n }\n children.push(newChild || child)\n }\n\n if (changed) {\n return Fragment.from(children)\n }\n\n return\n}\n\nfunction replaceMarkInNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode | undefined {\n if (node.type.spec.code) {\n return\n }\n if (node.type.isInline) {\n return\n }\n if (node.type.isTextblock) {\n return replaceMarkInTextblockNode(options, node)\n }\n\n const newChildren = replaceMarkInFragment(options, node.content)\n if (!newChildren) {\n return\n }\n return node.copy(newChildren)\n}\n\nfunction replaceMarkInTextblockNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode | undefined {\n const newChildren: ProseMirrorNode[] = []\n let changed = false\n\n for (const inlineNode of node.content.content) {\n const newInlineNodes = replaceMarkInInlineNode(options, inlineNode)\n if (newInlineNodes) {\n changed = true\n newChildren.push(...newInlineNodes)\n } else {\n newChildren.push(inlineNode)\n }\n }\n if (changed) {\n return node.copy(Fragment.from(newChildren))\n }\n return\n}\n\nfunction replaceMarkInInlineNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode[] | undefined {\n const text = node.text\n if (!text) {\n return\n }\n\n const { markType, shouldSkip } = options\n\n // Use custom skip logic if provided, otherwise use default\n if (shouldSkip) {\n if (shouldSkip(node)) {\n return\n }\n } else {\n // Default skip logic: skip if already has the target mark or has code mark\n if (node.marks.some((mark) => mark.type === markType)) {\n return\n }\n if (node.marks.some((mark) => mark.type.spec.code)) {\n return\n }\n }\n\n const chunks = splitTextByRegex(text, options.regex)\n if (!chunks) {\n return\n }\n\n const schema = node.type.schema\n const nodes: ProseMirrorNode[] = []\n\n for (const [text, match] of chunks) {\n if (!text) {\n continue\n }\n if (match) {\n const attrs = options.getAttrs?.(match) ?? null\n if (attrs !== false) {\n const mark = markType.create(attrs)\n nodes.push(schema.text(text, [...node.marks, mark]))\n } else {\n nodes.push(schema.text(text, node.marks))\n }\n } else {\n nodes.push(schema.text(text, node.marks))\n }\n }\n\n return nodes\n}\n"],"mappings":";;;;;;;;AAmBA,MAAM,iBAAiB,YAA6C;CAClE,cAAc;EACZ,IAAIA,WAA+B,EAAE;EAErC,MAAM,mBAAmB,OAAc,MAAkB,UAA0B;AACjF,QAAK,MAAM,WAAW,SACpB,SAAQ,QAAQ;IAAE;IAAO;IAAM;IAAO,CAAC;AAEzC,UAAO;;EAGT,MAAM,SAAS,IAAI,kBAAkB;GACnC,KAAK,IAAI,UAAU,sBAAsB;GACzC,OAAO,EACL,iBACD;GACF,CAAC;AAEF,UAAQ,WAA+B;AAErC,cAAW,CAAC,GAAG,OAAO,CAAC,SAAS;AAChC,UAAO;;;CAGX,WAAW;CACX,QAAQ;CACT,CAAC;;;;AAKF,SAAgB,sBAAsB,SAA2C;AAC/E,QAAO,mBAAmB,gBAAgB,CAAC,QAAQ,CAAC;;;;;;;;;;;;;ACMtD,SAAgB,gBAAgB,EAAE,WAA6C;AAC7E,QAAO,sBAAsB,QAAQ;;;;;;;;;;ACrDvC,SAAgB,iBACd,MACA,OAC0D;AAC1D,OAAM,YAAY;CAElB,MAAMC,SAAuD,EAAE;CAC/D,IAAI,YAAY;CAChB,IAAIC;CACJ,IAA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AM,CAAC;QAG3C,OAAM,KAAK,OAAO,KAAKA,QAAM,KAAK,MAAM,CAAC;;AAI7C,QAAO"}
|
|
1
|
+
{"version":3,"file":"paste-rule-BIztzELg.js","names":["handlers: PasteRulePayload[]","chunks: Array<[string, RegExpExecArray | undefined]>","match: RegExpExecArray | null","children: ProseMirrorNode[]","newChildren: ProseMirrorNode[]","nodes: ProseMirrorNode[]","text"],"sources":["../src/paste-rule/paste-rule-plugin.ts","../src/paste-rule/paste-rule.ts","../src/paste-rule/split-text-by-regex.ts","../src/paste-rule/mark-paste-rule.ts"],"sourcesContent":["import {\n defineFacet,\n defineFacetPayload,\n pluginFacet,\n type PlainExtension,\n type PluginPayload,\n} from '@prosekit/core'\nimport type { Slice } from '@prosekit/pm/model'\nimport {\n PluginKey,\n ProseMirrorPlugin,\n} from '@prosekit/pm/state'\nimport type { EditorView } from '@prosekit/pm/view'\n\ntype PasteRulePayload = (options: { slice: Slice; view: EditorView; plain: boolean }) => Slice\n\n/**\n * @internal\n */\nconst pasteRuleFacet = defineFacet<PasteRulePayload, PluginPayload>({\n reduce: () => {\n let handlers: PasteRulePayload[] = []\n\n const transformPasted = (slice: Slice, view: EditorView, plain: boolean): Slice => {\n for (const handler of handlers) {\n slice = handler({ slice, view, plain })\n }\n return slice\n }\n\n const plugin = new ProseMirrorPlugin({\n key: new PluginKey('prosekit-paste-rule'),\n props: {\n transformPasted,\n },\n })\n\n return (inputs: PasteRulePayload[]) => {\n // Last added rule (highest priority) is applied first\n handlers = [...inputs].reverse()\n return plugin\n }\n },\n singleton: true,\n parent: pluginFacet,\n})\n\n/**\n * @internal\n */\nexport function definePasteRulePlugin(payload: PasteRulePayload): PlainExtension {\n return defineFacetPayload(pasteRuleFacet, [payload]) as PlainExtension\n}\n","import type { PlainExtension } from '@prosekit/core'\nimport type { Slice } from '@prosekit/pm/model'\nimport type { EditorView } from '@prosekit/pm/view'\n\nimport { definePasteRulePlugin } from './paste-rule-plugin'\n\n/**\n * @public\n *\n * Options for {@link PasteRuleHandler}.\n */\nexport interface PasteRuleHandlerOptions {\n /**\n * The slice to be pasted.\n */\n slice: Slice\n\n /**\n * The editor view.\n */\n view: EditorView\n\n /**\n * Whether the pasted content is treated as plain text. This is true when the\n * `Shift` key is held when pasting.\n */\n plain: boolean\n}\n\n/**\n * @public\n *\n * Can be used to transform pasted or dragged-and-dropped content before it is\n * applied to the document.\n */\nexport type PasteRuleHandler = (options: PasteRuleHandlerOptions) => Slice\n\n/**\n * Options for {@link definePasteRule}.\n *\n * @public\n */\nexport interface PasteRuleOptions {\n /**\n * A function to be called when a paste rule is triggered.\n */\n handler: PasteRuleHandler\n}\n\n/**\n * Defines a paste rule. This rule allows you to modify pasted or dragged\n * content before it is inserted into the document.\n *\n * @param options\n *\n * @public\n */\nexport function definePasteRule({ handler }: PasteRuleOptions): PlainExtension {\n return definePasteRulePlugin(handler)\n}\n","/**\n * Splits text into chunks based on regex matches, preserving both matched and unmatched segments.\n * Returns an array of tuples where each tuple contains a text segment and either the match data\n * (for matched segments) or undefined (for unmatched segments).\n */\nexport function splitTextByRegex(\n text: string,\n regex: RegExp,\n): Array<[string, RegExpExecArray | undefined]> | undefined {\n regex.lastIndex = 0\n\n const chunks: Array<[string, RegExpExecArray | undefined]> = []\n let lastIndex = 0\n let match: RegExpExecArray | null\n let matched = false\n\n while ((match = regex.exec(text))) {\n const start = match.index\n const end = regex.lastIndex\n\n // Push the unmatched prefix, if any.\n if (start > lastIndex) {\n chunks.push([text.slice(lastIndex, start), undefined])\n }\n\n // Push the matched segment.\n chunks.push([text.slice(start, end), match])\n matched = true\n\n if (lastIndex === end) {\n // Safeguard against zero-width matches that would otherwise cause an infinite loop.\n return\n }\n lastIndex = end\n }\n\n if (matched && lastIndex < text.length) {\n chunks.push([text.slice(lastIndex), undefined])\n }\n\n regex.lastIndex = 0\n\n return matched ? chunks : undefined\n}\n","import {\n getMarkType,\n type PlainExtension,\n} from '@prosekit/core'\nimport type {\n Attrs,\n MarkType,\n ProseMirrorNode,\n} from '@prosekit/pm/model'\nimport {\n Fragment,\n Slice,\n} from '@prosekit/pm/model'\n\nimport { definePasteRule } from './paste-rule'\nimport { splitTextByRegex } from './split-text-by-regex'\n\n/**\n * The options for {@link defineMarkPasteRule}.\n *\n * @public\n */\nexport interface MarkPasteRuleOptions {\n /**\n * The regular expression to match against. It must have a `g` flag to match\n * all instances of the mark.\n */\n regex: RegExp\n\n /**\n * The mark type to apply to the matched text.\n */\n type: string | MarkType\n\n /**\n * A function used to compute attributes to set on the mark created by this\n * rule. When it returns `false`, the rule won't match. When it returns `null`\n * or `undefined`, that is interpreted as an empty/default set of attributes.\n * @default null\n */\n getAttrs?: (match: RegExpExecArray) => Attrs | null | undefined | false\n\n /**\n * Optional function to determine if a text node should be skipped.\n * Default behavior: skip code nodes and nodes that already have the target mark.\n */\n shouldSkip?: (node: ProseMirrorNode) => boolean\n}\n\n/**\n * Defines a paste rule that applies marks based on regex patterns.\n *\n * @public\n */\nexport function defineMarkPasteRule(options: MarkPasteRuleOptions): PlainExtension {\n return definePasteRule({\n handler: ({ slice, view, plain }) => {\n if (plain) {\n return slice\n }\n\n const markType = getMarkType(view.state.schema, options.type)\n\n return replaceMarkInSlice({\n markType,\n regex: options.regex,\n getAttrs: options.getAttrs,\n shouldSkip: options.shouldSkip,\n }, slice)\n },\n })\n}\n\ninterface MarkPasteRuleHandlerOptions {\n markType: MarkType\n regex: RegExp\n getAttrs?: (match: RegExpExecArray) => Attrs | null | undefined | false\n shouldSkip?: (node: ProseMirrorNode) => boolean\n}\n\nfunction replaceMarkInSlice(options: MarkPasteRuleHandlerOptions, slice: Slice): Slice {\n const newFragment = replaceMarkInFragment(options, slice.content)\n if (!newFragment) {\n return slice\n }\n return new Slice(newFragment, slice.openStart, slice.openEnd)\n}\n\nfunction replaceMarkInFragment(options: MarkPasteRuleHandlerOptions, fragment: Fragment): Fragment | undefined {\n let changed = false\n let children: ProseMirrorNode[] = []\n\n for (const child of fragment.content) {\n const newChild = replaceMarkInNode(options, child)\n if (newChild) {\n changed = true\n }\n children.push(newChild || child)\n }\n\n if (changed) {\n return Fragment.from(children)\n }\n\n return\n}\n\nfunction replaceMarkInNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode | undefined {\n if (node.type.spec.code) {\n return\n }\n if (node.type.isInline) {\n return\n }\n if (node.type.isTextblock) {\n return replaceMarkInTextblockNode(options, node)\n }\n\n const newChildren = replaceMarkInFragment(options, node.content)\n if (!newChildren) {\n return\n }\n return node.copy(newChildren)\n}\n\nfunction replaceMarkInTextblockNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode | undefined {\n const newChildren: ProseMirrorNode[] = []\n let changed = false\n\n for (const inlineNode of node.content.content) {\n const newInlineNodes = replaceMarkInInlineNode(options, inlineNode)\n if (newInlineNodes) {\n changed = true\n newChildren.push(...newInlineNodes)\n } else {\n newChildren.push(inlineNode)\n }\n }\n if (changed) {\n return node.copy(Fragment.from(newChildren))\n }\n return\n}\n\nfunction replaceMarkInInlineNode(options: MarkPasteRuleHandlerOptions, node: ProseMirrorNode): ProseMirrorNode[] | undefined {\n const text = node.text\n if (!text) {\n return\n }\n\n const { markType, shouldSkip } = options\n\n // Use custom skip logic if provided, otherwise use default\n if (shouldSkip) {\n if (shouldSkip(node)) {\n return\n }\n } else {\n // Default skip logic: skip if already has the target mark or has code mark\n if (node.marks.some((mark) => mark.type === markType)) {\n return\n }\n if (node.marks.some((mark) => mark.type.spec.code)) {\n return\n }\n }\n\n const chunks = splitTextByRegex(text, options.regex)\n if (!chunks) {\n return\n }\n\n const schema = node.type.schema\n const nodes: ProseMirrorNode[] = []\n\n for (const [text, match] of chunks) {\n if (!text) {\n continue\n }\n if (match) {\n const attrs = options.getAttrs?.(match) ?? null\n if (attrs !== false) {\n const mark = markType.create(attrs)\n nodes.push(schema.text(text, [...node.marks, mark]))\n } else {\n nodes.push(schema.text(text, node.marks))\n }\n } else {\n nodes.push(schema.text(text, node.mar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AAY,CAAC;;AAKhD,SAAS,wBAAwB,SAAsC,MAAsD;CAC3H,MAAM,OAAO,KAAK;AAClB,KAAI,CAAC,KACH;CAGF,MAAM,EAAE,UAAU,eAAe;AAGjC,KAAI,YACF;MAAI,WAAW,KAAK,CAClB;QAEG;AAEL,MAAI,KAAK,MAAM,MAAM,SAAS,KAAK,SAAS,SAAS,CACnD;AAEF,MAAI,KAAK,MAAM,MAAM,SAAS,KAAK,KAAK,KAAK,KAAK,CAChD;;CAIJ,MAAM,SAAS,iBAAiB,MAAM,QAAQ,MAAM;AACpD,KAAI,CAAC,OACH;CAGF,MAAM,SAAS,KAAK,KAAK;CACzB,MAAMC,QAA2B,EAAE;AAEnC,MAAK,MAAM,CAACC,QAAM,UAAU,QAAQ;AAClC,MAAI,CAACA,OACH;AAEF,MAAI,OAAO;GACT,MAAM,QAAQ,QAAQ,WAAW,MAAM,IAAI;AAC3C,OAAI,UAAU,OAAO;IACnB,MAAM,OAAO,SAAS,OAAO,MAAM;AACnC,UAAM,KAAK,OAAO,KAAKA,QAAM,CAAC,GAAG,KAAK,OAAO,KAAK,CAAC,CAAC;SAEpD,OAAM,KAAK,OAAO,KAAKA,QAAM,KAAK,MAAM,CAAC;QAG3C,OAAM,KAAK,OAAO,KAAKA,QAAM,KAAK,MAAM,CAAC;;AAI7C,QAAO"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ShikiHighlighterOptions } from "./shiki-highlighter-chunk-
|
|
1
|
+
import { ShikiHighlighterOptions } from "./shiki-highlighter-chunk-Cwu1Jr9o.js";
|
|
2
2
|
import { Extension, PlainExtension, Union } from "@prosekit/core";
|
|
3
3
|
import { Parser } from "prosemirror-highlight";
|
|
4
4
|
import { BundledLanguage as ShikiBundledLanguage, BundledLanguageInfo as ShikiBundledLanguageInfo, BundledTheme as ShikiBundledTheme, BundledThemeInfo as ShikiBundledThemeInfo, SpecialLanguage, bundledLanguagesInfo as shikiBundledLanguagesInfo, bundledThemesInfo as shikiBundledThemesInfo } from "shiki";
|
|
@@ -1,111 +1,8 @@
|
|
|
1
1
|
import { PlainExtension } from "@prosekit/core";
|
|
2
|
-
import {
|
|
3
|
-
import { EditorView } from "@prosekit/pm/view";
|
|
4
|
-
import { Slice } from "@prosekit/pm/model";
|
|
2
|
+
import { DragEventHandler, DragEventHandlerOptions, DropIndicatorPluginOptions, Line, Point, ShowHandler, ShowHandlerOptions, ViewDragging } from "prosemirror-drop-indicator";
|
|
5
3
|
|
|
6
|
-
//#region src/drop-indicator/types.d.ts
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* A function that will be called when the `dragover` event is fired. You can
|
|
10
|
-
* return `false` to disable the current drop point and thus hide the drop
|
|
11
|
-
* indicator.
|
|
12
|
-
*
|
|
13
|
-
* @public
|
|
14
|
-
*/
|
|
15
|
-
type DragEventHandler = (options: DragEventHandlerOptions) => boolean;
|
|
16
|
-
/**
|
|
17
|
-
* Options for {@link DragEventHandler}.
|
|
18
|
-
*
|
|
19
|
-
* @public
|
|
20
|
-
*/
|
|
21
|
-
interface DragEventHandlerOptions {
|
|
22
|
-
/**
|
|
23
|
-
* The editor's view.
|
|
24
|
-
*/
|
|
25
|
-
view: EditorView;
|
|
26
|
-
/**
|
|
27
|
-
* The drop position in current document.
|
|
28
|
-
*/
|
|
29
|
-
pos: number;
|
|
30
|
-
/**
|
|
31
|
-
* The `dragover` event.
|
|
32
|
-
*/
|
|
33
|
-
event: DragEvent;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* A function that will be called when the drop indicator should be shown.
|
|
37
|
-
*
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
type ShowHandler = (options: ShowHandlerOptions) => void;
|
|
41
|
-
/**
|
|
42
|
-
* Options for {@link ShowHandler}.
|
|
43
|
-
*
|
|
44
|
-
* @public
|
|
45
|
-
*/
|
|
46
|
-
interface ShowHandlerOptions {
|
|
47
|
-
/**
|
|
48
|
-
* The editor's view.
|
|
49
|
-
*/
|
|
50
|
-
view: EditorView;
|
|
51
|
-
/**
|
|
52
|
-
* The ProseMirror position that the drop indicator should be shown at.
|
|
53
|
-
*/
|
|
54
|
-
pos: number;
|
|
55
|
-
/**
|
|
56
|
-
* The line that the drop indicator should be shown at.
|
|
57
|
-
*/
|
|
58
|
-
line: Line;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* @internal
|
|
62
|
-
*/
|
|
63
|
-
interface Point {
|
|
64
|
-
readonly x: number;
|
|
65
|
-
readonly y: number;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* @internal
|
|
69
|
-
*/
|
|
70
|
-
interface Line {
|
|
71
|
-
readonly p1: Point;
|
|
72
|
-
readonly p2: Point;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* An interface matching the internal ProseMirror implementation.
|
|
76
|
-
*
|
|
77
|
-
* See https://github.com/ProseMirror/prosemirror-view/blob/1.38.1/src/input.ts#L657
|
|
78
|
-
*
|
|
79
|
-
* @internal
|
|
80
|
-
*/
|
|
81
|
-
interface ViewDragging {
|
|
82
|
-
readonly slice: Slice;
|
|
83
|
-
readonly move: boolean;
|
|
84
|
-
readonly node?: NodeSelection;
|
|
85
|
-
}
|
|
86
|
-
//#endregion
|
|
87
|
-
//#region src/drop-indicator/drop-indicator-facet.d.ts
|
|
88
|
-
/**
|
|
89
|
-
* @internal
|
|
90
|
-
*/
|
|
91
|
-
interface DropIndicatorPayload {
|
|
92
|
-
/**
|
|
93
|
-
* A callback that is called when the drop indicator should be shown.
|
|
94
|
-
*/
|
|
95
|
-
onShow?: ShowHandler;
|
|
96
|
-
/**
|
|
97
|
-
* A callback that is called when the drop indicator should be hidden.
|
|
98
|
-
*/
|
|
99
|
-
onHide?: VoidFunction;
|
|
100
|
-
/**
|
|
101
|
-
* A callback that is called when the `dragover` event is fired. You can
|
|
102
|
-
* return `false` to disable the current drop point and thus hide the drop
|
|
103
|
-
* indicator.
|
|
104
|
-
*/
|
|
105
|
-
onDrag?: DragEventHandler;
|
|
106
|
-
}
|
|
107
|
-
//#endregion
|
|
108
4
|
//#region src/drop-indicator/drop-indicator.d.ts
|
|
5
|
+
|
|
109
6
|
/**
|
|
110
7
|
* @internal
|
|
111
8
|
*/
|
|
@@ -129,7 +26,7 @@ declare function defineDropIndicator(options?: DropIndicatorOptions): DropIndica
|
|
|
129
26
|
*
|
|
130
27
|
* @public
|
|
131
28
|
*/
|
|
132
|
-
interface DropIndicatorOptions extends
|
|
29
|
+
interface DropIndicatorOptions extends DropIndicatorPluginOptions {}
|
|
133
30
|
//#endregion
|
|
134
31
|
export { type DragEventHandler, type DragEventHandlerOptions, type DropIndicatorExtension, type DropIndicatorOptions, type Line, type Point, type ShowHandler, type ShowHandlerOptions, type ViewDragging, defineDropIndicator };
|
|
135
32
|
//# sourceMappingURL=prosekit-extensions-drop-indicator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prosekit-extensions-drop-indicator.d.ts","names":[],"sources":["../src/drop-indicator/
|
|
1
|
+
{"version":3,"file":"prosekit-extensions-drop-indicator.d.ts","names":[],"sources":["../src/drop-indicator/drop-indicator.ts"],"sourcesContent":[],"mappings":";;;;;;;AAQA;AAegB,KAfJ,sBAAA,GAAyB,cAeF;;;;;AAWnC;;;;;;;;;iBAXgB,mBAAA,WACJ,uBACT;;;;;;UASc,oBAAA,SAA6B"}
|