1 import DialogBox from "./DialogBox";
2 import DialogForm from "./DialogForm";
3 import DialogInput from "./DialogInput";
4 import DialogRadioOptions from "./DialogRadioOptions";
5 import schema from "../schema";
7 import {MenuItem} from "./menu";
8 import {icons} from "./icons";
11 * @param {PmMarkType} markType
12 * @param {String} attribute
13 * @return {(function(PmEditorState): (string|null))}
15 function getMarkAttribute(markType, attribute) {
16 return function (state) {
17 const marks = state.selection.$head.marks();
18 for (const mark of marks) {
19 if (mark.type === markType) {
20 return mark.attrs[attribute];
29 * @param {(function(FormData))} submitter
30 * @param {Function} closer
33 function getLinkDialog(submitter, closer) {
34 return new DialogBox([
39 value: getMarkAttribute(schema.marks.link, 'href'),
44 value: getMarkAttribute(schema.marks.link, 'title'),
46 new DialogRadioOptions({
47 "Same tab or window": "",
48 "New tab or window": "_blank",
52 value: getMarkAttribute(schema.marks.link, 'target'),
65 * @param {FormData} formData
66 * @param {PmEditorState} state
67 * @param {PmDispatchFunction} dispatch
70 function applyLink(formData, state, dispatch) {
71 const selection = state.selection;
72 const attrs = Object.fromEntries(formData);
77 tr.addMark(selection.from, selection.to, schema.marks.link.create(attrs));
79 tr.removeMark(selection.from, selection.to, schema.marks.link);
87 * @param {PmEditorState} state
88 * @param {PmDispatchFunction} dispatch
89 * @param {PmView} view
92 function onPress(state, dispatch, view, e) {
93 const dialog = getLinkDialog((data) => {
94 applyLink(data, state, dispatch);
100 const {dom, update} = dialog.render(view);
102 document.body.appendChild(dom);
108 function anchorButtonItem() {
109 return new MenuItem({
110 title: "Insert/Edit Anchor Link",
112 enable: state => true,
117 export default anchorButtonItem;