rhino-editor
Version:
A custom element wrapped rich text editor
102 lines (99 loc) • 3.16 kB
JavaScript
import {
figureTypes
} from "./chunk-2NYF3SXX.js";
// src/exports/extensions/gallery.ts
import { mergeAttributes, Node, selectionToInsertionEnd } from "@tiptap/core";
import { Plugin } from "@tiptap/pm/state";
import {
chainCommands,
createParagraphNear
} from "@tiptap/pm/commands";
import { findParentNodeOfTypeClosestToPos } from "prosemirror-utils";
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 = Node.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-DY6CH2YC.js.map