1
0
mirror of https://github.com/writeas/writefreely synced 2024-12-12 16:48:09 +01:00
writefreely/prose/menu.js

33 lines
843 B
JavaScript
Raw Normal View History

import { MenuItem } from "prosemirror-menu";
import { buildMenuItems } from "prosemirror-example-setup";
import { writeFreelySchema } from "./schema";
function canInsert(state, nodeType, attrs) {
2021-01-08 00:33:35 +01:00
let $from = state.selection.$from;
for (let d = $from.depth; d >= 0; d--) {
let index = $from.index(d);
if ($from.node(d).canReplaceWith(index, index, nodeType, attrs))
return true;
}
2021-01-08 00:33:35 +01:00
return false;
}
const ReadMoreItem = new MenuItem({
2021-01-08 00:33:35 +01:00
label: "Read more",
select: (state) => canInsert(state, writeFreelySchema.nodes.readmore),
run(state, dispatch) {
dispatch(
state.tr.replaceSelectionWith(writeFreelySchema.nodes.readmore.create())
);
},
});
2021-01-08 00:33:35 +01:00
export const getMenu = () => {
const menuContent = [
...buildMenuItems(writeFreelySchema).fullMenu,
[ReadMoreItem],
];
return menuContent;
};