1 import DialogBox from "./DialogBox";
2 import DialogForm from "./DialogForm";
3 import DialogInput from "./DialogInput";
4 import schema from "../schema";
6 import {MenuItem} from "./menu";
7 import {icons} from "./icons";
10 * @param {PmMarkType} markType
11 * @param {String} attribute
12 * @return {(function(PmEditorState): (string|null))}
14 function getMarkAttribute(markType, attribute) {
15 return function (state) {
16 const marks = state.selection.$head.marks();
17 for (const mark of marks) {
18 if (mark.type === markType) {
19 return mark.attrs[attribute];
28 * @param {(function(FormData))} submitter
29 * @param {Function} closer
32 function getLinkDialog(submitter, closer) {
33 return new DialogBox([
38 value: getMarkAttribute(schema.marks.link, 'href'),
43 value: getMarkAttribute(schema.marks.link, 'title'),
48 value: getMarkAttribute(schema.marks.link, 'target'),
61 * @param {FormData} formData
62 * @param {PmEditorState} state
63 * @param {PmDispatchFunction} dispatch
66 function applyLink(formData, state, dispatch) {
67 const selection = state.selection;
68 const attrs = Object.fromEntries(formData);
73 tr.addMark(selection.from, selection.to, schema.marks.link.create(attrs));
75 tr.removeMark(selection.from, selection.to, schema.marks.link);
83 * @param {PmEditorState} state
84 * @param {PmDispatchFunction} dispatch
85 * @param {PmView} view
88 function onPress(state, dispatch, view, e) {
89 const dialog = getLinkDialog((data) => {
90 applyLink(data, state, dispatch);
96 const {dom, update} = dialog.render(view);
98 document.body.appendChild(dom);
104 function anchorButtonItem() {
105 return new MenuItem({
106 title: "Insert/Edit Anchor Link",
108 enable: state => true,
113 export default anchorButtonItem;