Initial
This commit is contained in:
37
resources/app/common/prosemirror/paste-transformer.mjs
Normal file
37
resources/app/common/prosemirror/paste-transformer.mjs
Normal file
@@ -0,0 +1,37 @@
|
||||
import ProseMirrorPlugin from "./plugin.mjs";
|
||||
import { Plugin } from "prosemirror-state";
|
||||
import { randomID } from "../utils/helpers.mjs";
|
||||
import { transformSlice } from "./util.mjs";
|
||||
|
||||
/**
|
||||
* A class responsible for applying transformations to content pasted inside the editor.
|
||||
*/
|
||||
export default class ProseMirrorPasteTransformer extends ProseMirrorPlugin {
|
||||
/** @override */
|
||||
static build(schema, options={}) {
|
||||
const plugin = new ProseMirrorPasteTransformer(schema);
|
||||
return new Plugin({
|
||||
props: {
|
||||
transformPasted: plugin._onPaste.bind(plugin)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Transform content before it is injected into the ProseMirror document.
|
||||
* @param {Slice} slice The content slice.
|
||||
* @param {EditorView} view The ProseMirror editor view.
|
||||
* @returns {Slice} The transformed content.
|
||||
*/
|
||||
_onPaste(slice, view) {
|
||||
// Give pasted secret blocks new IDs.
|
||||
const secret = view.state.schema.nodes.secret;
|
||||
return transformSlice(slice, node => {
|
||||
if ( node.type === secret ) {
|
||||
return secret.create({ ...node.attrs, id: `secret-${randomID()}` }, node.content, node.marks);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user