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";
8 import {nullifyEmptyValues} from "../util";
11 * @param {PmNodeType} nodeType
12 * @param {String} attribute
13 * @return {(function(PmEditorState): (string|null))}
15 function getNodeAttribute(nodeType, attribute) {
16 return function (state) {
17 const node = state.selection.node;
18 if (node && node.type === nodeType) {
19 return node.attrs[attribute];
27 * @param {(function(FormData))} submitter
28 * @param {Function} closer
31 function getLinkDialog(submitter, closer) {
32 return new DialogBox([
37 value: getNodeAttribute(schema.nodes.iframe, 'src'),
42 value: getNodeAttribute(schema.nodes.iframe, 'title'),
47 value: getNodeAttribute(schema.nodes.iframe, 'width'),
52 value: getNodeAttribute(schema.nodes.iframe, 'height'),
59 label: 'Insert Embedded Content',
65 * @param {FormData} formData
66 * @param {PmEditorState} state
67 * @param {PmDispatchFunction} dispatch
70 function applyIframe(formData, state, dispatch) {
71 const attrs = nullifyEmptyValues(Object.fromEntries(formData));
72 if (!dispatch) return true;
75 const currentNodeAttrs = state.selection?.nodes?.attrs || {};
76 const newAttrs = Object.assign({}, currentNodeAttrs, attrs);
77 tr.replaceSelectionWith(schema.nodes.iframe.create(newAttrs));
84 * @param {PmEditorState} state
85 * @param {PmDispatchFunction} dispatch
86 * @param {PmView} view
89 function onPress(state, dispatch, view, e) {
90 const dialog = getLinkDialog((data) => {
91 applyIframe(data, state, dispatch);
97 const {dom, update} = dialog.render(view);
99 document.body.appendChild(dom);
105 function iframeButtonItem() {
106 return new MenuItem({
107 title: "Embed Content",
109 enable: state => true,
114 export default iframeButtonItem;