@milkdown/plugin-upload 7.0.0-next.0 → 7.0.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/default-uploader.d.ts.map +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.es.js +2 -2
- package/lib/index.es.js.map +1 -1
- package/lib/upload.d.ts +5 -5
- package/lib/upload.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/default-uploader.ts +3 -0
- package/src/index.ts +3 -2
- package/src/upload.ts +22 -5
package/README.md
CHANGED
|
@@ -8,4 +8,4 @@ Documentation can be found on the [Milkdown website](https://milkdown.dev/plugin
|
|
|
8
8
|
|
|
9
9
|
# License
|
|
10
10
|
|
|
11
|
-
Milkdown is open sourced software licensed under [MIT license](https://github.com/
|
|
11
|
+
Milkdown is open sourced software licensed under [MIT license](https://github.com/Milkdown/milkdown/blob/main/LICENSE).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"default-uploader.d.ts","sourceRoot":"","sources":["../src/default-uploader.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"default-uploader.d.ts","sourceRoot":"","sources":["../src/default-uploader.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAGxC,eAAO,MAAM,iBAAiB,SAAU,IAAI,KAAG,QAAQ;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAelF,CAAA;AAID,eAAO,MAAM,eAAe,EAAE,QAqB7B,CAAA"}
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAGnD,cAAc,UAAU,CAAA;AACxB,cAAc,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAGnD,cAAc,UAAU,CAAA;AACxB,cAAc,oBAAoB,CAAA;AAGlC,eAAO,MAAM,MAAM,EAAE,cAAc,EAAiC,CAAA"}
|
package/lib/index.es.js
CHANGED
|
@@ -96,8 +96,8 @@ const A = (a) => new Promise((i) => {
|
|
|
96
96
|
export {
|
|
97
97
|
E as defaultUploader,
|
|
98
98
|
A as readImageAsBase64,
|
|
99
|
-
M as updatePlugin,
|
|
100
99
|
I as upload,
|
|
101
|
-
l as uploadConfig
|
|
100
|
+
l as uploadConfig,
|
|
101
|
+
M as uploadPlugin
|
|
102
102
|
};
|
|
103
103
|
//# sourceMappingURL=index.es.js.map
|
package/lib/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/default-uploader.ts","../src/upload.ts","../src/index.ts"],"sourcesContent":["/* Copyright 2021, Milkdown by Mirone. */\nimport { missingNodeInSchema } from '@milkdown/exception'\nimport type { Node } from '@milkdown/prose/model'\n\nimport type { Uploader } from './upload'\n\nexport const readImageAsBase64 = (file: File): Promise<{ alt: string; src: string }> => {\n return new Promise((resolve) => {\n const reader = new FileReader()\n reader.addEventListener(\n 'load',\n () => {\n resolve({\n alt: file.name,\n src: reader.result as string,\n })\n },\n false,\n )\n reader.readAsDataURL(file)\n })\n}\n\nexport const defaultUploader: Uploader = async (files, schema) => {\n const imgs: File[] = []\n\n for (let i = 0; i < files.length; i++) {\n const file = files.item(i)\n if (!file)\n continue\n\n if (!file.type.includes('image'))\n continue\n\n imgs.push(file)\n }\n\n const { image } = schema.nodes\n if (!image)\n throw missingNodeInSchema('image')\n\n const data = await Promise.all(imgs.map(img => readImageAsBase64(img)))\n\n return data.map(({ alt, src }) => image.createAndFill({ src, alt }) as Node)\n}\n","/* Copyright 2021, Milkdown by Mirone. */\nimport { schemaCtx } from '@milkdown/core'\nimport type { Fragment, Node, Schema } from '@milkdown/prose/model'\nimport type { EditorState } from '@milkdown/prose/state'\nimport { Plugin, PluginKey } from '@milkdown/prose/state'\nimport type { EditorView } from '@milkdown/prose/view'\nimport { Decoration, DecorationSet } from '@milkdown/prose/view'\nimport { $ctx, $prose } from '@milkdown/utils'\n\nimport { defaultUploader } from './default-uploader'\n\nexport type Uploader = (files: FileList, schema: Schema) => Promise<Fragment | Node | Node[]>\ninterface Spec { id: symbol; pos: number }\nexport interface UploadConfig {\n uploader: Uploader\n enableHtmlFileUploader: boolean\n uploadWidgetFactory: (pos: number, spec: Parameters<typeof Decoration.widget>[2]) => Decoration\n}\n\nexport const uploadConfig = $ctx<UploadConfig, 'uploadConfig'>({\n uploader: defaultUploader,\n enableHtmlFileUploader: false,\n uploadWidgetFactory: (pos, spec) => {\n const widgetDOM = document.createElement('span')\n widgetDOM.textContent = 'Upload in progress...'\n return Decoration.widget(pos, widgetDOM, spec)\n },\n}, 'uploadConfig')\n\nexport const updatePlugin = $prose((ctx) => {\n const pluginKey = new PluginKey('MILKDOWN_UPLOAD')\n\n const findPlaceholder = (state: EditorState, id: symbol): number => {\n const decorations = pluginKey.getState(state)\n if (!decorations)\n return -1\n const found = decorations.find(undefined, undefined, (spec: Spec) => spec.id === id)\n if (!found.length)\n return -1\n return found[0]?.from ?? -1\n }\n\n const handleUpload = (view: EditorView, event: DragEvent | ClipboardEvent, files: FileList | undefined) => {\n if (!files || files.length <= 0)\n return false\n\n const id = Symbol('upload symbol')\n const schema = ctx.get(schemaCtx)\n const { tr } = view.state\n const insertPos = event instanceof DragEvent\n ? view.posAtCoords({ left: event.clientX, top: event.clientY })?.pos ?? tr.selection.from\n : tr.selection.from\n view.dispatch(tr.setMeta(pluginKey, { add: { id, pos: insertPos } }))\n\n const { uploader } = ctx.get(uploadConfig.key)\n uploader(files, schema)\n .then((fragment) => {\n const pos = findPlaceholder(view.state, id)\n if (pos < 0)\n return\n\n view.dispatch(\n view.state.tr\n .replaceWith(pos, pos, fragment)\n .setMeta(pluginKey, { remove: { id } }),\n )\n })\n .catch((e) => {\n console.error(e)\n })\n return true\n }\n\n return new Plugin({\n key: pluginKey,\n state: {\n init() {\n return DecorationSet.empty\n },\n apply(this: Plugin, tr, set) {\n const _set = set.map(tr.mapping, tr.doc)\n const action = tr.getMeta(this)\n if (!action)\n return _set\n\n if (action.add) {\n const { uploadWidgetFactory } = ctx.get(uploadConfig.key)\n\n const decoration = uploadWidgetFactory(action.add.pos, { id: action.add.id })\n return _set.add(tr.doc, [decoration])\n }\n if (action.remove) {\n const target = _set.find(undefined, undefined, (spec: Spec) => spec.id === action.remove.id)\n return _set.remove(target)\n }\n\n return _set\n },\n },\n props: {\n decorations(this: Plugin, state) {\n return this.getState(state)\n },\n handlePaste: (view, event) => {\n const { enableHtmlFileUploader } = ctx.get(uploadConfig.key)\n if (!(event instanceof ClipboardEvent))\n return false\n\n if (!enableHtmlFileUploader && event.clipboardData?.getData('text/html'))\n return false\n\n return handleUpload(view, event, event.clipboardData?.files)\n },\n handleDrop: (view, event) => {\n if (!(event instanceof DragEvent))\n return false\n\n return handleUpload(view, event, event.dataTransfer?.files)\n },\n },\n })\n})\n","/* Copyright 2021, Milkdown by Mirone. */\nimport type { MilkdownPlugin } from '@milkdown/ctx'\nimport { updatePlugin, uploadConfig } from './upload'\n\nexport * from './upload'\nexport * from './default-uploader'\n\nexport const upload: MilkdownPlugin[] = [uploadConfig, updatePlugin]\n"],"names":["readImageAsBase64","file","resolve","reader","defaultUploader","files","schema","imgs","i","image","missingNodeInSchema","img","alt","src","uploadConfig","$ctx","pos","spec","widgetDOM","Decoration","updatePlugin","$prose","ctx","pluginKey","PluginKey","findPlaceholder","state","id","decorations","found","_a","handleUpload","view","event","schemaCtx","tr","insertPos","uploader","fragment","e","Plugin","DecorationSet","set","_set","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CAAY;"}
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/default-uploader.ts","../src/upload.ts","../src/index.ts"],"sourcesContent":["/* Copyright 2021, Milkdown by Mirone. */\nimport { missingNodeInSchema } from '@milkdown/exception'\nimport type { Node } from '@milkdown/prose/model'\n\nimport type { Uploader } from './upload'\n\n/// Read the image file as base64.\nexport const readImageAsBase64 = (file: File): Promise<{ alt: string; src: string }> => {\n return new Promise((resolve) => {\n const reader = new FileReader()\n reader.addEventListener(\n 'load',\n () => {\n resolve({\n alt: file.name,\n src: reader.result as string,\n })\n },\n false,\n )\n reader.readAsDataURL(file)\n })\n}\n\n/// The default uploader.\n/// It will upload transform images to base64.\nexport const defaultUploader: Uploader = async (files, schema) => {\n const imgs: File[] = []\n\n for (let i = 0; i < files.length; i++) {\n const file = files.item(i)\n if (!file)\n continue\n\n if (!file.type.includes('image'))\n continue\n\n imgs.push(file)\n }\n\n const { image } = schema.nodes\n if (!image)\n throw missingNodeInSchema('image')\n\n const data = await Promise.all(imgs.map(img => readImageAsBase64(img)))\n\n return data.map(({ alt, src }) => image.createAndFill({ src, alt }) as Node)\n}\n","/* Copyright 2021, Milkdown by Mirone. */\nimport { schemaCtx } from '@milkdown/core'\nimport type { Fragment, Node, Schema } from '@milkdown/prose/model'\nimport type { EditorState } from '@milkdown/prose/state'\nimport { Plugin, PluginKey } from '@milkdown/prose/state'\nimport type { EditorView } from '@milkdown/prose/view'\nimport { Decoration, DecorationSet } from '@milkdown/prose/view'\nimport { $ctx, $prose } from '@milkdown/utils'\n\nimport { defaultUploader } from './default-uploader'\n\n/// @internal\nexport type Uploader = UploadOptions['uploader']\ninterface Spec { id: symbol; pos: number }\n\n/// The configuration for upload.\nexport interface UploadOptions {\n /// The uploader for upload plugin.\n /// It takes the files and schema as parameters.\n /// It should return a `Promise` of Prosemirror `Fragment` or `Node` or `Node[]`.\n uploader: (files: FileList, schema: Schema) => Promise<Fragment | Node | Node[]>\n /// Whether to enable the html file uploader.\n /// When paste files from html (for example copy images by right click context menu),\n /// this option will make the plugin to upload the image copied instead of using the original link.\n enableHtmlFileUploader: boolean\n /// The factory for upload widget.\n /// The widget will be displayed when the file is uploading.\n /// It takes the position and spec as parameters.\n /// It should return a `Decoration` of Prosemirror.\n /// By default, it will return `<span>Upload in progress...</span>`.\n uploadWidgetFactory: (pos: number, spec: Parameters<typeof Decoration.widget>[2]) => Decoration\n}\n\n/// A slice that contains the configuration for upload.\n/// It should be type of `UploadConfig`.\nexport const uploadConfig = $ctx<UploadOptions, 'uploadConfig'>({\n uploader: defaultUploader,\n enableHtmlFileUploader: false,\n uploadWidgetFactory: (pos, spec) => {\n const widgetDOM = document.createElement('span')\n widgetDOM.textContent = 'Upload in progress...'\n return Decoration.widget(pos, widgetDOM, spec)\n },\n}, 'uploadConfig')\n\n/// The prosemirror plugin for upload.\nexport const uploadPlugin = $prose((ctx) => {\n const pluginKey = new PluginKey('MILKDOWN_UPLOAD')\n\n const findPlaceholder = (state: EditorState, id: symbol): number => {\n const decorations = pluginKey.getState(state)\n if (!decorations)\n return -1\n const found = decorations.find(undefined, undefined, (spec: Spec) => spec.id === id)\n if (!found.length)\n return -1\n return found[0]?.from ?? -1\n }\n\n const handleUpload = (view: EditorView, event: DragEvent | ClipboardEvent, files: FileList | undefined) => {\n if (!files || files.length <= 0)\n return false\n\n const id = Symbol('upload symbol')\n const schema = ctx.get(schemaCtx)\n const { tr } = view.state\n const insertPos = event instanceof DragEvent\n ? view.posAtCoords({ left: event.clientX, top: event.clientY })?.pos ?? tr.selection.from\n : tr.selection.from\n view.dispatch(tr.setMeta(pluginKey, { add: { id, pos: insertPos } }))\n\n const { uploader } = ctx.get(uploadConfig.key)\n uploader(files, schema)\n .then((fragment) => {\n const pos = findPlaceholder(view.state, id)\n if (pos < 0)\n return\n\n view.dispatch(\n view.state.tr\n .replaceWith(pos, pos, fragment)\n .setMeta(pluginKey, { remove: { id } }),\n )\n })\n .catch((e) => {\n console.error(e)\n })\n return true\n }\n\n return new Plugin({\n key: pluginKey,\n state: {\n init() {\n return DecorationSet.empty\n },\n apply(this: Plugin, tr, set) {\n const _set = set.map(tr.mapping, tr.doc)\n const action = tr.getMeta(this)\n if (!action)\n return _set\n\n if (action.add) {\n const { uploadWidgetFactory } = ctx.get(uploadConfig.key)\n\n const decoration = uploadWidgetFactory(action.add.pos, { id: action.add.id })\n return _set.add(tr.doc, [decoration])\n }\n if (action.remove) {\n const target = _set.find(undefined, undefined, (spec: Spec) => spec.id === action.remove.id)\n return _set.remove(target)\n }\n\n return _set\n },\n },\n props: {\n decorations(this: Plugin, state) {\n return this.getState(state)\n },\n handlePaste: (view, event) => {\n const { enableHtmlFileUploader } = ctx.get(uploadConfig.key)\n if (!(event instanceof ClipboardEvent))\n return false\n\n if (!enableHtmlFileUploader && event.clipboardData?.getData('text/html'))\n return false\n\n return handleUpload(view, event, event.clipboardData?.files)\n },\n handleDrop: (view, event) => {\n if (!(event instanceof DragEvent))\n return false\n\n return handleUpload(view, event, event.dataTransfer?.files)\n },\n },\n })\n})\n","/* Copyright 2021, Milkdown by Mirone. */\nimport type { MilkdownPlugin } from '@milkdown/ctx'\nimport { uploadConfig, uploadPlugin } from './upload'\n\nexport * from './upload'\nexport * from './default-uploader'\n\n/// All plugins exported by this package.\nexport const upload: MilkdownPlugin[] = [uploadConfig, uploadPlugin]\n"],"names":["readImageAsBase64","file","resolve","reader","defaultUploader","files","schema","imgs","i","image","missingNodeInSchema","img","alt","src","uploadConfig","$ctx","pos","spec","widgetDOM","Decoration","uploadPlugin","$prose","ctx","pluginKey","PluginKey","findPlaceholder","state","id","decorations","found","_a","handleUpload","view","event","schemaCtx","tr","insertPos","uploader","fragment","e","Plugin","DecorationSet","set","_set","action","uploadWidgetFactory","decoration","target","enableHtmlFileUploader","_b","upload"],"mappings":";;;;;AAOa,MAAAA,IAAoB,CAACC,MACzB,IAAI,QAAQ,CAACC,MAAY;AACxB,QAAAC,IAAS,IAAI;AACZ,EAAAA,EAAA;AAAA,IACL;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AACI,iBAAAD;AAET,YAAIC,EAAO,KAAK;AACd,gBAAM,EAAE,qBAAAC,EAAoB,IAAIvB,EAAI,IAAIR,EAAa,GAAG,GAElDgC,IAAaD,EAAoBD,EAAO,IAAI,KAAK,EAAE,IAAIA,EAAO,IAAI,GAAI,CAAA;AAC5E,iBAAOD,EAAK,IAAIR,EAAG,KAAK,CAACW,CAAU,CAAC;AAAA,QACtC;AACA,YAAIF,EAAO,QAAQ;AACX,gBAAAG,IAASJ,EAAK,KAAK,QAAW,QAAW,CAAC1B,MAAeA,EAAK,OAAO2B,EAAO,OAAO,EAAE;AACpF,iBAAAD,EAAK,OAAOI,CAAM;AAAA,QAC3B;AAEO,eAAAJ;AAAA,MACT;AAAA,IACF;AAAA,IACA,OAAO;AAAA,MACL,YAA0BjB,GAAO;AACxB,eAAA,KAAK,SAASA,CAAK;AAAA,MAC5B;AAAA,MACA,aAAa,CAACM,GAAMC,MAAU;;AAC5B,cAAM,EAAE,wBAAAe,EAAuB,IAAI1B,EAAI,IAAIR,EAAa,GAAG;AAI3D,eAHI,EAAEmB,aAAiB,mBAGnB,CAACe,OAA0BlB,IAAAG,EAAM,kBAAN,gBAAAH,EAAqB,QAAQ,gBACnD,KAEFC,EAAaC,GAAMC,IAAOgB,IAAAhB,EAAM,kBAAN,gBAAAgB,EAAqB,KAAK;AAAA,MAC7D;AAAA,MACA,YAAY,CAACjB,GAAMC,MAAU;;AAC3B,eAAMA,aAAiB,YAGhBF,EAAaC,GAAMC,IAAOH,IAAAG,EAAM,iBAAN,gBAAAH,EAAoB,KAAK,IAFjD;AAAA,MAGX;AAAA,IACF;AAAA,EAAA,CACD;AACH,CAAC,GClIYoB,IAA2B,CAACpC,GAAcM,CAAY;"}
|
package/lib/upload.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Fragment, Node, Schema } from '@milkdown/prose/model';
|
|
2
2
|
import { Decoration } from '@milkdown/prose/view';
|
|
3
|
-
export type Uploader =
|
|
4
|
-
export interface
|
|
5
|
-
uploader:
|
|
3
|
+
export type Uploader = UploadOptions['uploader'];
|
|
4
|
+
export interface UploadOptions {
|
|
5
|
+
uploader: (files: FileList, schema: Schema) => Promise<Fragment | Node | Node[]>;
|
|
6
6
|
enableHtmlFileUploader: boolean;
|
|
7
7
|
uploadWidgetFactory: (pos: number, spec: Parameters<typeof Decoration.widget>[2]) => Decoration;
|
|
8
8
|
}
|
|
9
|
-
export declare const uploadConfig: import("@milkdown/utils").$Ctx<
|
|
10
|
-
export declare const
|
|
9
|
+
export declare const uploadConfig: import("@milkdown/utils").$Ctx<UploadOptions, "uploadConfig">;
|
|
10
|
+
export declare const uploadPlugin: import("@milkdown/utils").$Prose;
|
|
11
11
|
//# sourceMappingURL=upload.d.ts.map
|
package/lib/upload.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../src/upload.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAInE,OAAO,EAAE,UAAU,EAAiB,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../src/upload.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAInE,OAAO,EAAE,UAAU,EAAiB,MAAM,sBAAsB,CAAA;AAMhE,MAAM,MAAM,QAAQ,GAAG,aAAa,CAAC,UAAU,CAAC,CAAA;AAIhD,MAAM,WAAW,aAAa;IAI5B,QAAQ,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,GAAG,IAAI,GAAG,IAAI,EAAE,CAAC,CAAA;IAIhF,sBAAsB,EAAE,OAAO,CAAA;IAM/B,mBAAmB,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,UAAU,CAAA;CAChG;AAID,eAAO,MAAM,YAAY,+DAQP,CAAA;AAGlB,eAAO,MAAM,YAAY,kCA4FvB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkdown/plugin-upload",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.0.0-next.
|
|
4
|
+
"version": "7.0.0-next.2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/
|
|
8
|
+
"url": "git+https://github.com/Milkdown/milkdown.git",
|
|
9
9
|
"directory": "packages/plugin-upload"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"tslib": "^2.4.0",
|
|
29
|
-
"@milkdown/exception": "7.0.0-next.
|
|
30
|
-
"@milkdown/utils": "7.0.0-next.
|
|
29
|
+
"@milkdown/exception": "7.0.0-next.2",
|
|
30
|
+
"@milkdown/utils": "7.0.0-next.2"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@milkdown/core": "7.0.0-next.
|
|
34
|
-
"@milkdown/ctx": "7.0.0-next.
|
|
35
|
-
"@milkdown/prose": "7.0.0-next.
|
|
33
|
+
"@milkdown/core": "7.0.0-next.2",
|
|
34
|
+
"@milkdown/ctx": "7.0.0-next.2",
|
|
35
|
+
"@milkdown/prose": "7.0.0-next.2"
|
|
36
36
|
},
|
|
37
37
|
"nx": {
|
|
38
38
|
"targets": {
|
package/src/default-uploader.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { Node } from '@milkdown/prose/model'
|
|
|
4
4
|
|
|
5
5
|
import type { Uploader } from './upload'
|
|
6
6
|
|
|
7
|
+
/// Read the image file as base64.
|
|
7
8
|
export const readImageAsBase64 = (file: File): Promise<{ alt: string; src: string }> => {
|
|
8
9
|
return new Promise((resolve) => {
|
|
9
10
|
const reader = new FileReader()
|
|
@@ -21,6 +22,8 @@ export const readImageAsBase64 = (file: File): Promise<{ alt: string; src: strin
|
|
|
21
22
|
})
|
|
22
23
|
}
|
|
23
24
|
|
|
25
|
+
/// The default uploader.
|
|
26
|
+
/// It will upload transform images to base64.
|
|
24
27
|
export const defaultUploader: Uploader = async (files, schema) => {
|
|
25
28
|
const imgs: File[] = []
|
|
26
29
|
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
2
|
import type { MilkdownPlugin } from '@milkdown/ctx'
|
|
3
|
-
import {
|
|
3
|
+
import { uploadConfig, uploadPlugin } from './upload'
|
|
4
4
|
|
|
5
5
|
export * from './upload'
|
|
6
6
|
export * from './default-uploader'
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/// All plugins exported by this package.
|
|
9
|
+
export const upload: MilkdownPlugin[] = [uploadConfig, uploadPlugin]
|
package/src/upload.ts
CHANGED
|
@@ -9,15 +9,31 @@ import { $ctx, $prose } from '@milkdown/utils'
|
|
|
9
9
|
|
|
10
10
|
import { defaultUploader } from './default-uploader'
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/// @internal
|
|
13
|
+
export type Uploader = UploadOptions['uploader']
|
|
13
14
|
interface Spec { id: symbol; pos: number }
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
|
|
16
|
+
/// The configuration for upload.
|
|
17
|
+
export interface UploadOptions {
|
|
18
|
+
/// The uploader for upload plugin.
|
|
19
|
+
/// It takes the files and schema as parameters.
|
|
20
|
+
/// It should return a `Promise` of Prosemirror `Fragment` or `Node` or `Node[]`.
|
|
21
|
+
uploader: (files: FileList, schema: Schema) => Promise<Fragment | Node | Node[]>
|
|
22
|
+
/// Whether to enable the html file uploader.
|
|
23
|
+
/// When paste files from html (for example copy images by right click context menu),
|
|
24
|
+
/// this option will make the plugin to upload the image copied instead of using the original link.
|
|
16
25
|
enableHtmlFileUploader: boolean
|
|
26
|
+
/// The factory for upload widget.
|
|
27
|
+
/// The widget will be displayed when the file is uploading.
|
|
28
|
+
/// It takes the position and spec as parameters.
|
|
29
|
+
/// It should return a `Decoration` of Prosemirror.
|
|
30
|
+
/// By default, it will return `<span>Upload in progress...</span>`.
|
|
17
31
|
uploadWidgetFactory: (pos: number, spec: Parameters<typeof Decoration.widget>[2]) => Decoration
|
|
18
32
|
}
|
|
19
33
|
|
|
20
|
-
|
|
34
|
+
/// A slice that contains the configuration for upload.
|
|
35
|
+
/// It should be type of `UploadConfig`.
|
|
36
|
+
export const uploadConfig = $ctx<UploadOptions, 'uploadConfig'>({
|
|
21
37
|
uploader: defaultUploader,
|
|
22
38
|
enableHtmlFileUploader: false,
|
|
23
39
|
uploadWidgetFactory: (pos, spec) => {
|
|
@@ -27,7 +43,8 @@ export const uploadConfig = $ctx<UploadConfig, 'uploadConfig'>({
|
|
|
27
43
|
},
|
|
28
44
|
}, 'uploadConfig')
|
|
29
45
|
|
|
30
|
-
|
|
46
|
+
/// The prosemirror plugin for upload.
|
|
47
|
+
export const uploadPlugin = $prose((ctx) => {
|
|
31
48
|
const pluginKey = new PluginKey('MILKDOWN_UPLOAD')
|
|
32
49
|
|
|
33
50
|
const findPlaceholder = (state: EditorState, id: symbol): number => {
|