UNPKG

rhino-editor

Version:

A custom element wrapped rich text editor

106 lines (103 loc) 3.1 kB
import { figureTypes, findParentNodeOfTypeClosestToPos } from "./chunk-SDYFI7HW.js"; import { Node3, chainCommands, createParagraphNear, mergeAttributes, selectionToInsertionEnd } from "./chunk-4CJ3F2XK.js"; import { Plugin } from "./chunk-XG3W5CFT.js"; // src/exports/extensions/gallery.ts function replaceEmptyGalleryWithParagraph(node, tr, newState, pos) { let modified = false; if (node.type.name != "attachment-gallery") return modified; if (node.nodeSize === 2) { tr.replaceWith( pos, pos + node.nodeSize, newState.schema.node("paragraph", null, []) ); modified = true; } return modified; } var Gallery = Node3.create({ name: "attachment-gallery", group: "block", draggable: false, selectable: false, content: "(paragraph | previewableAttachmentFigure)*", parseHTML() { return [ { tag: "div.attachment-gallery" } ]; }, renderHTML() { return ["div", mergeAttributes({}, { class: "attachment-gallery" }), 0]; }, addProseMirrorPlugins() { return [ new Plugin({ props: { handleDOMEvents: { keydown: (view, event) => { if (event.key === "Enter") { const nodeType = view.state.selection.$head.parent.type.name; if (nodeType === "attachment-gallery") { event.preventDefault(); chainCommands(createParagraphNear)(view.state, view.dispatch); return true; } if (figureTypes.includes(nodeType)) { event.preventDefault(); chainCommands(createParagraphNear)(view.state, view.dispatch); const containingGallery = findParentNodeOfTypeClosestToPos( view.state.selection.$anchor, view.state.schema.nodes["attachment-gallery"] ); if (containingGallery) { const tr = view.state.tr; tr.insert( containingGallery.pos + containingGallery.node.nodeSize, view.state.schema.nodes["paragraph"].create() ); selectionToInsertionEnd(tr, tr.steps.length - 1, -1); view.dispatch(tr); } return true; } } return false; } } }, appendTransaction: (_transactions, _oldState, newState) => { const tr = newState.tr; let modified = false; newState.doc.descendants((node, pos, _parent) => { const mutations = [ replaceEmptyGalleryWithParagraph(node, tr, newState, pos) ]; const shouldModify = mutations.some((bool) => bool === true); if (shouldModify) { modified = true; } }); if (modified) return tr; return void 0; } }) ]; } }); export { Gallery }; //# sourceMappingURL=chunk-PYMFSMQK.js.map