@tiptap/extension-file-handler 2.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/dist/FileHandlePlugin.d.ts +4 -0
- package/dist/FileHandlePlugin.d.ts.map +1 -0
- package/dist/fileHandler.d.ts +3 -0
- package/dist/fileHandler.d.ts.map +1 -0
- package/dist/index.cjs +90 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +93 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/types.d.ts +36 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +48 -0
- package/src/FileHandlePlugin.ts +82 -0
- package/src/fileHandler.ts +25 -0
- package/src/index.ts +8 -0
- package/src/types.ts +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# @tiptap/extension-file-handler
|
|
2
|
+
[](https://www.npmjs.com/package/@tiptap/extension-file-handler)
|
|
3
|
+
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
|
4
|
+
[](https://www.npmjs.com/package/@tiptap/extension-file-handler)
|
|
5
|
+
[](https://github.com/sponsors/ueberdosis)
|
|
6
|
+
|
|
7
|
+
## Introduction
|
|
8
|
+
Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
|
|
9
|
+
|
|
10
|
+
## Official Documentation
|
|
11
|
+
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Plugin } from '@tiptap/pm/state';
|
|
2
|
+
import { FileHandlePluginOptions } from './types.js';
|
|
3
|
+
export declare const FileHandlePlugin: ({ key, editor, onPaste, onDrop, allowedMimeTypes, }: FileHandlePluginOptions) => Plugin<any>;
|
|
4
|
+
//# sourceMappingURL=FileHandlePlugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FileHandlePlugin.d.ts","sourceRoot":"","sources":["../src/FileHandlePlugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAa,MAAM,kBAAkB,CAAA;AAEpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAEpD,eAAO,MAAM,gBAAgB,wDAE1B,uBAAuB,gBA2EzB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fileHandler.d.ts","sourceRoot":"","sources":["../src/fileHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAMxC,eAAO,MAAM,WAAW,sFAkBtB,CAAA"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@tiptap/core');
|
|
6
|
+
var state = require('@tiptap/pm/state');
|
|
7
|
+
|
|
8
|
+
const FileHandlePlugin = ({ key, editor, onPaste, onDrop, allowedMimeTypes, }) => {
|
|
9
|
+
return new state.Plugin({
|
|
10
|
+
key: key || new state.PluginKey('fileHandler'),
|
|
11
|
+
props: {
|
|
12
|
+
handleDrop(_view, event) {
|
|
13
|
+
var _a;
|
|
14
|
+
if (!onDrop) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
if (!((_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.files.length)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const dropPos = _view.posAtCoords({
|
|
21
|
+
left: event.clientX,
|
|
22
|
+
top: event.clientY,
|
|
23
|
+
});
|
|
24
|
+
let filesArray = Array.from(event.dataTransfer.files);
|
|
25
|
+
if (allowedMimeTypes) {
|
|
26
|
+
filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type));
|
|
27
|
+
}
|
|
28
|
+
if (filesArray.length === 0) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
event.preventDefault();
|
|
32
|
+
event.stopPropagation();
|
|
33
|
+
onDrop(editor, filesArray, (dropPos === null || dropPos === void 0 ? void 0 : dropPos.pos) || 0);
|
|
34
|
+
return true;
|
|
35
|
+
},
|
|
36
|
+
handlePaste(_view, event) {
|
|
37
|
+
var _a;
|
|
38
|
+
if (!onPaste) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (!((_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.files.length)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
let filesArray = Array.from(event.clipboardData.files);
|
|
45
|
+
const htmlContent = event.clipboardData.getData('text/html');
|
|
46
|
+
if (allowedMimeTypes) {
|
|
47
|
+
filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type));
|
|
48
|
+
}
|
|
49
|
+
if (filesArray.length === 0) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
event.stopPropagation();
|
|
54
|
+
onPaste(editor, filesArray, htmlContent);
|
|
55
|
+
// if there is also file data inside the clipboard html,
|
|
56
|
+
// we won't use the files array and instead get the file url from the html
|
|
57
|
+
// this mostly happens for gifs or webms as they are not copied correctly as a file
|
|
58
|
+
// and will always be transformed into a PNG
|
|
59
|
+
// in this case we will let other extensions handle the incoming html via their inputRules
|
|
60
|
+
if (htmlContent.length > 0) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const FileHandler = core.Extension.create({
|
|
70
|
+
name: 'fileHandler',
|
|
71
|
+
addOptions() {
|
|
72
|
+
return {
|
|
73
|
+
onPaste: undefined,
|
|
74
|
+
onDrop: undefined,
|
|
75
|
+
allowedMimeTypes: undefined,
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
addProseMirrorPlugins() {
|
|
79
|
+
return [
|
|
80
|
+
FileHandlePlugin({
|
|
81
|
+
key: new state.PluginKey(this.name), editor: this.editor, allowedMimeTypes: this.options.allowedMimeTypes, onDrop: this.options.onDrop, onPaste: this.options.onPaste,
|
|
82
|
+
}),
|
|
83
|
+
];
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
exports.FileHandlePlugin = FileHandlePlugin;
|
|
88
|
+
exports.FileHandler = FileHandler;
|
|
89
|
+
exports.default = FileHandler;
|
|
90
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/FileHandlePlugin.ts","../src/fileHandler.ts"],"sourcesContent":["import { Plugin, PluginKey } from '@tiptap/pm/state'\n\nimport { FileHandlePluginOptions } from './types.js'\n\nexport const FileHandlePlugin = ({\n key, editor, onPaste, onDrop, allowedMimeTypes,\n}: FileHandlePluginOptions) => {\n return new Plugin({\n key: key || new PluginKey('fileHandler'),\n\n props: {\n handleDrop(_view, event) {\n if (!onDrop) {\n return false\n }\n\n if (!event.dataTransfer?.files.length) {\n return false\n }\n\n const dropPos = _view.posAtCoords({\n left: event.clientX,\n top: event.clientY,\n })\n\n let filesArray = Array.from(event.dataTransfer.files)\n\n if (allowedMimeTypes) {\n filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type))\n }\n\n if (filesArray.length === 0) {\n return false\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n onDrop(editor, filesArray, dropPos?.pos || 0)\n\n return true\n },\n\n handlePaste(_view, event) {\n if (!onPaste) {\n return false\n }\n\n if (!event.clipboardData?.files.length) {\n return false\n }\n\n let filesArray = Array.from(event.clipboardData.files)\n const htmlContent = event.clipboardData.getData('text/html')\n\n if (allowedMimeTypes) {\n filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type))\n }\n\n if (filesArray.length === 0) {\n return false\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n onPaste(editor, filesArray, htmlContent)\n\n // if there is also file data inside the clipboard html,\n // we won't use the files array and instead get the file url from the html\n // this mostly happens for gifs or webms as they are not copied correctly as a file\n // and will always be transformed into a PNG\n // in this case we will let other extensions handle the incoming html via their inputRules\n if (htmlContent.length > 0) {\n return false\n }\n\n return true\n },\n },\n })\n}\n","import { Extension } from '@tiptap/core'\nimport { PluginKey } from '@tiptap/pm/state'\n\nimport { FileHandlePlugin } from './FileHandlePlugin.js'\nimport { FileHandlerOptions } from './types.js'\n\nexport const FileHandler = Extension.create<FileHandlerOptions>({\n name: 'fileHandler',\n\n addOptions() {\n return {\n onPaste: undefined,\n onDrop: undefined,\n allowedMimeTypes: undefined,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n FileHandlePlugin({\n key: new PluginKey(this.name), editor: this.editor, allowedMimeTypes: this.options.allowedMimeTypes, onDrop: this.options.onDrop, onPaste: this.options.onPaste,\n }),\n ]\n },\n})\n"],"names":["Plugin","PluginKey","Extension"],"mappings":";;;;;;;AAIa,MAAA,gBAAgB,GAAG,CAAC,EAC/B,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,gBAAgB,GACtB,KAAI;IAC5B,OAAO,IAAIA,YAAM,CAAC;AAChB,QAAA,GAAG,EAAE,GAAG,IAAI,IAAIC,eAAS,CAAC,aAAa,CAAC;AAExC,QAAA,KAAK,EAAE;YACL,UAAU,CAAC,KAAK,EAAE,KAAK,EAAA;;gBACrB,IAAI,CAAC,MAAM,EAAE;AACX,oBAAA,OAAO,KAAK;;AAGd,gBAAA,IAAI,EAAC,CAAA,EAAA,GAAA,KAAK,CAAC,YAAY,MAAE,I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":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAE9C,cAAc,uBAAuB,CAAA;AACrC,cAAc,YAAY,CAAA;AAE1B,OAAO,EAAE,WAAW,EAAE,CAAA;AAEtB,eAAe,WAAW,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Extension } from '@tiptap/core';
|
|
2
|
+
import { Plugin, PluginKey } from '@tiptap/pm/state';
|
|
3
|
+
|
|
4
|
+
const FileHandlePlugin = ({ key, editor, onPaste, onDrop, allowedMimeTypes, }) => {
|
|
5
|
+
return new Plugin({
|
|
6
|
+
key: key || new PluginKey('fileHandler'),
|
|
7
|
+
props: {
|
|
8
|
+
handleDrop(_view, event) {
|
|
9
|
+
var _a;
|
|
10
|
+
if (!onDrop) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (!((_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.files.length)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const dropPos = _view.posAtCoords({
|
|
17
|
+
left: event.clientX,
|
|
18
|
+
top: event.clientY,
|
|
19
|
+
});
|
|
20
|
+
let filesArray = Array.from(event.dataTransfer.files);
|
|
21
|
+
if (allowedMimeTypes) {
|
|
22
|
+
filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type));
|
|
23
|
+
}
|
|
24
|
+
if (filesArray.length === 0) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
event.preventDefault();
|
|
28
|
+
event.stopPropagation();
|
|
29
|
+
onDrop(editor, filesArray, (dropPos === null || dropPos === void 0 ? void 0 : dropPos.pos) || 0);
|
|
30
|
+
return true;
|
|
31
|
+
},
|
|
32
|
+
handlePaste(_view, event) {
|
|
33
|
+
var _a;
|
|
34
|
+
if (!onPaste) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if (!((_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.files.length)) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
let filesArray = Array.from(event.clipboardData.files);
|
|
41
|
+
const htmlContent = event.clipboardData.getData('text/html');
|
|
42
|
+
if (allowedMimeTypes) {
|
|
43
|
+
filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type));
|
|
44
|
+
}
|
|
45
|
+
if (filesArray.length === 0) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
event.stopPropagation();
|
|
50
|
+
onPaste(editor, filesArray, htmlContent);
|
|
51
|
+
// if there is also file data inside the clipboard html,
|
|
52
|
+
// we won't use the files array and instead get the file url from the html
|
|
53
|
+
// this mostly happens for gifs or webms as they are not copied correctly as a file
|
|
54
|
+
// and will always be transformed into a PNG
|
|
55
|
+
// in this case we will let other extensions handle the incoming html via their inputRules
|
|
56
|
+
if (htmlContent.length > 0) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const FileHandler = Extension.create({
|
|
66
|
+
name: 'fileHandler',
|
|
67
|
+
addOptions() {
|
|
68
|
+
return {
|
|
69
|
+
onPaste: undefined,
|
|
70
|
+
onDrop: undefined,
|
|
71
|
+
allowedMimeTypes: undefined,
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
addProseMirrorPlugins() {
|
|
75
|
+
return [
|
|
76
|
+
FileHandlePlugin({
|
|
77
|
+
key: new PluginKey(this.name), editor: this.editor, allowedMimeTypes: this.options.allowedMimeTypes, onDrop: this.options.onDrop, onPaste: this.options.onPaste,
|
|
78
|
+
}),
|
|
79
|
+
];
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
export { FileHandlePlugin, FileHandler, FileHandler as default };
|
|
84
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/FileHandlePlugin.ts","../src/fileHandler.ts"],"sourcesContent":["import { Plugin, PluginKey } from '@tiptap/pm/state'\n\nimport { FileHandlePluginOptions } from './types.js'\n\nexport const FileHandlePlugin = ({\n key, editor, onPaste, onDrop, allowedMimeTypes,\n}: FileHandlePluginOptions) => {\n return new Plugin({\n key: key || new PluginKey('fileHandler'),\n\n props: {\n handleDrop(_view, event) {\n if (!onDrop) {\n return false\n }\n\n if (!event.dataTransfer?.files.length) {\n return false\n }\n\n const dropPos = _view.posAtCoords({\n left: event.clientX,\n top: event.clientY,\n })\n\n let filesArray = Array.from(event.dataTransfer.files)\n\n if (allowedMimeTypes) {\n filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type))\n }\n\n if (filesArray.length === 0) {\n return false\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n onDrop(editor, filesArray, dropPos?.pos || 0)\n\n return true\n },\n\n handlePaste(_view, event) {\n if (!onPaste) {\n return false\n }\n\n if (!event.clipboardData?.files.length) {\n return false\n }\n\n let filesArray = Array.from(event.clipboardData.files)\n const htmlContent = event.clipboardData.getData('text/html')\n\n if (allowedMimeTypes) {\n filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type))\n }\n\n if (filesArray.length === 0) {\n return false\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n onPaste(editor, filesArray, htmlContent)\n\n // if there is also file data inside the clipboard html,\n // we won't use the files array and instead get the file url from the html\n // this mostly happens for gifs or webms as they are not copied correctly as a file\n // and will always be transformed into a PNG\n // in this case we will let other extensions handle the incoming html via their inputRules\n if (htmlContent.length > 0) {\n return false\n }\n\n return true\n },\n },\n })\n}\n","import { Extension } from '@tiptap/core'\nimport { PluginKey } from '@tiptap/pm/state'\n\nimport { FileHandlePlugin } from './FileHandlePlugin.js'\nimport { FileHandlerOptions } from './types.js'\n\nexport const FileHandler = Extension.create<FileHandlerOptions>({\n name: 'fileHandler',\n\n addOptions() {\n return {\n onPaste: undefined,\n onDrop: undefined,\n allowedMimeTypes: undefined,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n FileHandlePlugin({\n key: new PluginKey(this.name), editor: this.editor, allowedMimeTypes: this.options.allowedMimeTypes, onDrop: this.options.onDrop, onPaste: this.options.onPaste,\n }),\n ]\n },\n})\n"],"names":[],"mappings":";;;AAIa,MAAA,gBAAgB,GAAG,CAAC,EAC/B,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,gBAAgB,GACtB,KAAI;IAC5B,OAAO,IAAI,MAAM,CAAC;AAChB,QAAA,GAAG,EAAE,GAAG,IAAI,IAAI,SAAS,CAAC,aAAa,CAAC;AAExC,QAAA,KAAK,EAAE;YACL,UAAU,CAAC,KAAK,EAAE,KAAK,EAAA;;gBACrB,IAAI,CAAC,MAAM,EAAE;AACX,oBAAA,OAAO,KAAK;;AAGd,gBAAA,IAAI,EAAC,CAAA,EAAA,GAAA,KAAK,CAAC,YAAY,MAAE,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,KAAK,CAAC,MAAM,CAAA,EAAE;AACrC,oBAAA,OAAO,KAAK;;AAGd,gBAAA,MAAM,OAAO,GAAG,KAAK,CAAC,WAAW,CAAC;oBAChC,IAAI,EAAE,KAAK,CAAC,OAAO;oBACnB,GAAG,EAAE,KAAK,CAAC,OAAO;AACnB,iBAAA,CAAC;AAEF,gBAAA,IAAI,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC;gBAErD,IAAI,gBAAgB,EAAE;AACpB,oBAAA,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC,IAAI,IAAI,gBAAgB,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;;AAG9E,gBAAA,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAA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ACF,SAAA;AACF,KAAA,CAAC;AACJ;;AC3Ea,MAAA,WAAW,GAAG,SAAS,CAAC,MAAM,CAAqB;AAC9D,IAAA,IAAI,EAAE,aAAa;IAEnB,UAAU,GAAA;QACR,OAAO;AACL,YAAA,OAAO,EAAE,SAAS;AAClB,YAAA,MAAM,EAAE,SAAS;AACjB,YAAA,gBAAgB,EAAE,SAAS;SAC5B;KACF;IAED,qBAAqB,GAAA;QACnB,OAAO;AACL,YAAA,gBAAgB,CAAC;AACf,gBAAA,GAAG,EAAE,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,gBAAgB,EAAE,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO;aAChK,CAAC;SACH;KACF;AACF,CAAA;;;;"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tiptap/core'), require('@tiptap/pm/state')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@tiptap/core', '@tiptap/pm/state'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@tiptap/extension-file-handler"] = {}, global.core, global.state));
|
|
5
|
+
})(this, (function (exports, core, state) { 'use strict';
|
|
6
|
+
|
|
7
|
+
const FileHandlePlugin = ({ key, editor, onPaste, onDrop, allowedMimeTypes, }) => {
|
|
8
|
+
return new state.Plugin({
|
|
9
|
+
key: key || new state.PluginKey('fileHandler'),
|
|
10
|
+
props: {
|
|
11
|
+
handleDrop(_view, event) {
|
|
12
|
+
var _a;
|
|
13
|
+
if (!onDrop) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (!((_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.files.length)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const dropPos = _view.posAtCoords({
|
|
20
|
+
left: event.clientX,
|
|
21
|
+
top: event.clientY,
|
|
22
|
+
});
|
|
23
|
+
let filesArray = Array.from(event.dataTransfer.files);
|
|
24
|
+
if (allowedMimeTypes) {
|
|
25
|
+
filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type));
|
|
26
|
+
}
|
|
27
|
+
if (filesArray.length === 0) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
event.preventDefault();
|
|
31
|
+
event.stopPropagation();
|
|
32
|
+
onDrop(editor, filesArray, (dropPos === null || dropPos === void 0 ? void 0 : dropPos.pos) || 0);
|
|
33
|
+
return true;
|
|
34
|
+
},
|
|
35
|
+
handlePaste(_view, event) {
|
|
36
|
+
var _a;
|
|
37
|
+
if (!onPaste) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (!((_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.files.length)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
let filesArray = Array.from(event.clipboardData.files);
|
|
44
|
+
const htmlContent = event.clipboardData.getData('text/html');
|
|
45
|
+
if (allowedMimeTypes) {
|
|
46
|
+
filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type));
|
|
47
|
+
}
|
|
48
|
+
if (filesArray.length === 0) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
event.preventDefault();
|
|
52
|
+
event.stopPropagation();
|
|
53
|
+
onPaste(editor, filesArray, htmlContent);
|
|
54
|
+
// if there is also file data inside the clipboard html,
|
|
55
|
+
// we won't use the files array and instead get the file url from the html
|
|
56
|
+
// this mostly happens for gifs or webms as they are not copied correctly as a file
|
|
57
|
+
// and will always be transformed into a PNG
|
|
58
|
+
// in this case we will let other extensions handle the incoming html via their inputRules
|
|
59
|
+
if (htmlContent.length > 0) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const FileHandler = core.Extension.create({
|
|
69
|
+
name: 'fileHandler',
|
|
70
|
+
addOptions() {
|
|
71
|
+
return {
|
|
72
|
+
onPaste: undefined,
|
|
73
|
+
onDrop: undefined,
|
|
74
|
+
allowedMimeTypes: undefined,
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
addProseMirrorPlugins() {
|
|
78
|
+
return [
|
|
79
|
+
FileHandlePlugin({
|
|
80
|
+
key: new state.PluginKey(this.name), editor: this.editor, allowedMimeTypes: this.options.allowedMimeTypes, onDrop: this.options.onDrop, onPaste: this.options.onPaste,
|
|
81
|
+
}),
|
|
82
|
+
];
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
exports.FileHandlePlugin = FileHandlePlugin;
|
|
87
|
+
exports.FileHandler = FileHandler;
|
|
88
|
+
exports.default = FileHandler;
|
|
89
|
+
|
|
90
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
91
|
+
|
|
92
|
+
}));
|
|
93
|
+
//# sourceMappingURL=index.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/FileHandlePlugin.ts","../src/fileHandler.ts"],"sourcesContent":["import { Plugin, PluginKey } from '@tiptap/pm/state'\n\nimport { FileHandlePluginOptions } from './types.js'\n\nexport const FileHandlePlugin = ({\n key, editor, onPaste, onDrop, allowedMimeTypes,\n}: FileHandlePluginOptions) => {\n return new Plugin({\n key: key || new PluginKey('fileHandler'),\n\n props: {\n handleDrop(_view, event) {\n if (!onDrop) {\n return false\n }\n\n if (!event.dataTransfer?.files.length) {\n return false\n }\n\n const dropPos = _view.posAtCoords({\n left: event.clientX,\n top: event.clientY,\n })\n\n let filesArray = Array.from(event.dataTransfer.files)\n\n if (allowedMimeTypes) {\n filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type))\n }\n\n if (filesArray.length === 0) {\n return false\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n onDrop(editor, filesArray, dropPos?.pos || 0)\n\n return true\n },\n\n handlePaste(_view, event) {\n if (!onPaste) {\n return false\n }\n\n if (!event.clipboardData?.files.length) {\n return false\n }\n\n let filesArray = Array.from(event.clipboardData.files)\n const htmlContent = event.clipboardData.getData('text/html')\n\n if (allowedMimeTypes) {\n filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type))\n }\n\n if (filesArray.length === 0) {\n return false\n }\n\n event.preventDefault()\n event.stopPropagation()\n\n onPaste(editor, filesArray, htmlContent)\n\n // if there is also file data inside the clipboard html,\n // we won't use the files array and instead get the file url from the html\n // this mostly happens for gifs or webms as they are not copied correctly as a file\n // and will always be transformed into a PNG\n // in this case we will let other extensions handle the incoming html via their inputRules\n if (htmlContent.length > 0) {\n return false\n }\n\n return true\n },\n },\n })\n}\n","import { Extension } from '@tiptap/core'\nimport { PluginKey } from '@tiptap/pm/state'\n\nimport { FileHandlePlugin } from './FileHandlePlugin.js'\nimport { FileHandlerOptions } from './types.js'\n\nexport const FileHandler = Extension.create<FileHandlerOptions>({\n name: 'fileHandler',\n\n addOptions() {\n return {\n onPaste: undefined,\n onDrop: undefined,\n allowedMimeTypes: undefined,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n FileHandlePlugin({\n key: new PluginKey(this.name), editor: this.editor, allowedMimeTypes: this.options.allowedMimeTypes, onDrop: this.options.onDrop, onPaste: this.options.onPaste,\n }),\n ]\n },\n})\n"],"names":["Plugin","PluginKey","Extension"],"mappings":";;;;;;AAIa,QAAA,gBAAgB,GAAG,CAAC,EAC/B,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,gBAAgB,GACtB,KAAI;MAC5B,OAAO,IAAIA,YAAM,CAAC;EAChB,QAAA,GAAG,EAAE,GAAG,IAAI,IAAIC,eAAS,CAAC,aAAa,CAAC;EAEx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,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO;eAChK,CAAC;WACH;OACF;EACF,CAAA;;;;;;;;;;;;"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Editor } from '@tiptap/core';
|
|
2
|
+
import { PluginKey } from '@tiptap/pm/state';
|
|
3
|
+
export type FileHandlePluginOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* The plugin key.
|
|
6
|
+
* @default 'fileHandler'
|
|
7
|
+
*/
|
|
8
|
+
key?: PluginKey;
|
|
9
|
+
/**
|
|
10
|
+
* The editor instance.
|
|
11
|
+
*/
|
|
12
|
+
editor: Editor;
|
|
13
|
+
/**
|
|
14
|
+
* An array of allowed mimetypes
|
|
15
|
+
*
|
|
16
|
+
* example: ['image/jpeg', 'image/png']
|
|
17
|
+
*/
|
|
18
|
+
allowedMimeTypes?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* The onPaste callback that is called when a file is pasted.
|
|
21
|
+
* @param editor the editor instance
|
|
22
|
+
* @param files the File array including File objects
|
|
23
|
+
* @param pasteContent the pasted content as HTML string - this is only available if there is also html copied to the clipboard for example by copying from a website
|
|
24
|
+
* @returns Returns nothing.
|
|
25
|
+
*/
|
|
26
|
+
onPaste?: (editor: Editor, files: File[], pasteContent?: string) => void;
|
|
27
|
+
/**
|
|
28
|
+
* The onDrop callback that is called when a file is dropped.
|
|
29
|
+
* @param editor the editor instance
|
|
30
|
+
* @param files the File array including File objects
|
|
31
|
+
* @returns Returns nothing.
|
|
32
|
+
*/
|
|
33
|
+
onDrop?: (editor: Editor, files: File[], pos: number) => void;
|
|
34
|
+
};
|
|
35
|
+
export type FileHandlerOptions = {} & Omit<FileHandlePluginOptions, 'key' | 'editor'>;
|
|
36
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAE5C,MAAM,MAAM,uBAAuB,GAAG;IACpC;;;OAGG;IACH,GAAG,CAAC,EAAE,SAAS,CAAA;IAEf;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IAEd;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAA;IAE3B;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,YAAY,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;IAExE;;;;;OAKG;IACH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;CAC9D,CAAA;AAED,MAAM,MAAM,kBAAkB,GAAG,EAAE,GAAG,IAAI,CAAC,uBAAuB,EAAE,KAAK,GAAG,QAAQ,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tiptap/extension-file-handler",
|
|
3
|
+
"description": "file handler extension for tiptap",
|
|
4
|
+
"version": "2.22.0",
|
|
5
|
+
"homepage": "https://tiptap.dev/docs/editor/extensions/functionality/filehandler",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"tiptap",
|
|
8
|
+
"tiptap extension"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"funding": {
|
|
12
|
+
"type": "github",
|
|
13
|
+
"url": "https://github.com/sponsors/ueberdosis"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "dist/index.cjs",
|
|
24
|
+
"module": "dist/index.js",
|
|
25
|
+
"umd": "dist/index.umd.js",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"src",
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@tiptap/core": "^2.22.0",
|
|
33
|
+
"@tiptap/extension-text-style": "^2.22.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@tiptap/core": "^2.7.0",
|
|
37
|
+
"@tiptap/extension-text-style": "^2.7.0"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/ueberdosis/tiptap",
|
|
42
|
+
"directory": "packages/extension-file-handler"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"clean": "rm -rf dist",
|
|
46
|
+
"build": "npm run clean && rollup -c"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Plugin, PluginKey } from '@tiptap/pm/state'
|
|
2
|
+
|
|
3
|
+
import { FileHandlePluginOptions } from './types.js'
|
|
4
|
+
|
|
5
|
+
export const FileHandlePlugin = ({
|
|
6
|
+
key, editor, onPaste, onDrop, allowedMimeTypes,
|
|
7
|
+
}: FileHandlePluginOptions) => {
|
|
8
|
+
return new Plugin({
|
|
9
|
+
key: key || new PluginKey('fileHandler'),
|
|
10
|
+
|
|
11
|
+
props: {
|
|
12
|
+
handleDrop(_view, event) {
|
|
13
|
+
if (!onDrop) {
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!event.dataTransfer?.files.length) {
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const dropPos = _view.posAtCoords({
|
|
22
|
+
left: event.clientX,
|
|
23
|
+
top: event.clientY,
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
let filesArray = Array.from(event.dataTransfer.files)
|
|
27
|
+
|
|
28
|
+
if (allowedMimeTypes) {
|
|
29
|
+
filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (filesArray.length === 0) {
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
event.preventDefault()
|
|
37
|
+
event.stopPropagation()
|
|
38
|
+
|
|
39
|
+
onDrop(editor, filesArray, dropPos?.pos || 0)
|
|
40
|
+
|
|
41
|
+
return true
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
handlePaste(_view, event) {
|
|
45
|
+
if (!onPaste) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!event.clipboardData?.files.length) {
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let filesArray = Array.from(event.clipboardData.files)
|
|
54
|
+
const htmlContent = event.clipboardData.getData('text/html')
|
|
55
|
+
|
|
56
|
+
if (allowedMimeTypes) {
|
|
57
|
+
filesArray = filesArray.filter(file => allowedMimeTypes.includes(file.type))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (filesArray.length === 0) {
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
event.preventDefault()
|
|
65
|
+
event.stopPropagation()
|
|
66
|
+
|
|
67
|
+
onPaste(editor, filesArray, htmlContent)
|
|
68
|
+
|
|
69
|
+
// if there is also file data inside the clipboard html,
|
|
70
|
+
// we won't use the files array and instead get the file url from the html
|
|
71
|
+
// this mostly happens for gifs or webms as they are not copied correctly as a file
|
|
72
|
+
// and will always be transformed into a PNG
|
|
73
|
+
// in this case we will let other extensions handle the incoming html via their inputRules
|
|
74
|
+
if (htmlContent.length > 0) {
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return true
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Extension } from '@tiptap/core'
|
|
2
|
+
import { PluginKey } from '@tiptap/pm/state'
|
|
3
|
+
|
|
4
|
+
import { FileHandlePlugin } from './FileHandlePlugin.js'
|
|
5
|
+
import { FileHandlerOptions } from './types.js'
|
|
6
|
+
|
|
7
|
+
export const FileHandler = Extension.create<FileHandlerOptions>({
|
|
8
|
+
name: 'fileHandler',
|
|
9
|
+
|
|
10
|
+
addOptions() {
|
|
11
|
+
return {
|
|
12
|
+
onPaste: undefined,
|
|
13
|
+
onDrop: undefined,
|
|
14
|
+
allowedMimeTypes: undefined,
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
addProseMirrorPlugins() {
|
|
19
|
+
return [
|
|
20
|
+
FileHandlePlugin({
|
|
21
|
+
key: new PluginKey(this.name), editor: this.editor, allowedMimeTypes: this.options.allowedMimeTypes, onDrop: this.options.onDrop, onPaste: this.options.onPaste,
|
|
22
|
+
}),
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
})
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Editor } from '@tiptap/core'
|
|
2
|
+
import { PluginKey } from '@tiptap/pm/state'
|
|
3
|
+
|
|
4
|
+
export type FileHandlePluginOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* The plugin key.
|
|
7
|
+
* @default 'fileHandler'
|
|
8
|
+
*/
|
|
9
|
+
key?: PluginKey
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The editor instance.
|
|
13
|
+
*/
|
|
14
|
+
editor: Editor
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* An array of allowed mimetypes
|
|
18
|
+
*
|
|
19
|
+
* example: ['image/jpeg', 'image/png']
|
|
20
|
+
*/
|
|
21
|
+
allowedMimeTypes?: string[]
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The onPaste callback that is called when a file is pasted.
|
|
25
|
+
* @param editor the editor instance
|
|
26
|
+
* @param files the File array including File objects
|
|
27
|
+
* @param pasteContent the pasted content as HTML string - this is only available if there is also html copied to the clipboard for example by copying from a website
|
|
28
|
+
* @returns Returns nothing.
|
|
29
|
+
*/
|
|
30
|
+
onPaste?: (editor: Editor, files: File[], pasteContent?: string) => void
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The onDrop callback that is called when a file is dropped.
|
|
34
|
+
* @param editor the editor instance
|
|
35
|
+
* @param files the File array including File objects
|
|
36
|
+
* @returns Returns nothing.
|
|
37
|
+
*/
|
|
38
|
+
onDrop?: (editor: Editor, files: File[], pos: number) => void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type FileHandlerOptions = {} & Omit<FileHandlePluginOptions, 'key' | 'editor'>
|