2 * This file originates from https://github.com/ProseMirror/prosemirror-menu
3 * and is hence subject to the MIT license found here:
4 * https://github.com/ProseMirror/prosemirror-menu/blob/master/LICENSE
5 * @copyright Marijn Haverbeke and others
8 import crel from "crelt"
9 import {lift, joinUp, selectParentNode, wrapIn, setBlockType, toggleMark} from "prosemirror-commands"
10 import {undo, redo} from "prosemirror-history"
11 import {setBlockAttr} from "../commands";
13 import {getIcon} from "./icons"
15 const prefix = "ProseMirror-menu"
17 // ::- An icon or label that, when clicked, executes a command.
18 export class MenuItem {
22 // The spec used to create the menu item.
26 // :: (EditorView) → {dom: dom.Node, update: (EditorState) → bool}
27 // Renders the icon according to its [display
28 // spec](#menu.MenuItemSpec.display), and adds an event handler which
29 // executes the command when the representation is clicked.
32 let dom = spec.render ? spec.render(view)
33 : spec.icon ? getIcon(spec.icon)
34 : spec.label ? crel("div", null, translate(view, spec.label))
36 if (!dom) throw new RangeError("MenuItem without icon or label property")
38 const title = (typeof spec.title === "function" ? spec.title(view.state) : spec.title)
39 dom.setAttribute("title", translate(view, title))
41 if (spec.class) dom.classList.add(spec.class)
42 if (spec.css) dom.style.cssText += spec.css
44 dom.addEventListener("mousedown", e => {
46 if (!dom.classList.contains(prefix + "-disabled"))
47 spec.run(view.state, view.dispatch, view, e)
50 function update(state) {
52 let selected = spec.select(state)
53 dom.style.display = selected ? "" : "none"
54 if (!selected) return false
58 enabled = spec.enable(state) || false
59 setClass(dom, prefix + "-disabled", !enabled)
62 let active = enabled && spec.active(state) || false
63 setClass(dom, prefix + "-active", active)
72 function translate(view, text) {
73 return view._props.translate ? view._props.translate(text) : text
76 // MenuItemSpec:: interface
77 // The configuration object passed to the `MenuItem` constructor.
79 // run:: (EditorState, (Transaction), EditorView, dom.Event)
80 // The function to execute when the menu item is activated.
82 // select:: ?(EditorState) → bool
83 // Optional function that is used to determine whether the item is
84 // appropriate at the moment. Deselected items will be hidden.
86 // enable:: ?(EditorState) → bool
87 // Function that is used to determine if the item is enabled. If
88 // given and returning false, the item will be given a disabled
91 // active:: ?(EditorState) → bool
92 // A predicate function to determine whether the item is 'active' (for
93 // example, the item for toggling the strong mark might be active then
94 // the cursor is in strong text).
96 // render:: ?(EditorView) → dom.Node
97 // A function that renders the item. You must provide either this,
98 // [`icon`](#menu.MenuItemSpec.icon), or [`label`](#MenuItemSpec.label).
101 // Describes an icon to show for this item. The object may specify
102 // an SVG icon, in which case its `path` property should be an [SVG
104 // spec](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d),
105 // and `width` and `height` should provide the viewbox in which that
106 // path exists. Alternatively, it may have a `text` property
107 // specifying a string of text that makes up the icon, with an
108 // optional `css` property giving additional CSS styling for the
109 // text. _Or_ it may contain `dom` property containing a DOM node.
112 // Makes the item show up as a text label. Mostly useful for items
113 // wrapped in a [drop-down](#menu.Dropdown) or similar menu. The object
114 // should have a `label` property providing the text to display.
116 // title:: ?union<string, (EditorState) → string>
117 // Defines DOM title (mouseover) text for the item.
120 // Optionally adds a CSS class to the item's DOM representation.
123 // Optionally adds a string of inline CSS to the item's DOM
126 let lastMenuEvent = {time: 0, node: null}
127 function markMenuEvent(e) {
128 lastMenuEvent.time = Date.now()
129 lastMenuEvent.node = e.target
131 function isMenuEvent(wrapper) {
132 return Date.now() - 100 < lastMenuEvent.time &&
133 lastMenuEvent.node && wrapper.contains(lastMenuEvent.node)
136 // ::- A drop-down menu, displayed as a label with a downwards-pointing
137 // triangle to the right of it.
138 export class Dropdown {
139 // :: ([MenuElement], ?Object)
140 // Create a dropdown wrapping the elements. Options may include
141 // the following properties:
143 // **`label`**`: string`
144 // : The label to show on the drop-down control.
146 // **`title`**`: string`
148 // [`title`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/title)
149 // attribute given to the menu control.
151 // **`class`**`: string`
152 // : When given, adds an extra CSS class to the menu control.
154 // **`css`**`: string`
155 // : When given, adds an extra set of CSS styles to the menu control.
156 constructor(content, options) {
157 this.options = options || {}
158 this.content = Array.isArray(content) ? content : [content]
161 // :: (EditorView) → {dom: dom.Node, update: (EditorState)}
162 // Render the dropdown menu and sub-items.
164 let content = renderDropdownItems(this.content, view)
166 let label = crel("div", {class: prefix + "-dropdown " + (this.options.class || ""),
167 style: this.options.css},
168 translate(view, this.options.label))
169 if (this.options.title) label.setAttribute("title", translate(view, this.options.title))
170 let wrap = crel("div", {class: prefix + "-dropdown-wrap"}, label)
171 let open = null, listeningOnClose = null
173 if (open && open.close()) {
175 window.removeEventListener("mousedown", listeningOnClose)
178 label.addEventListener("mousedown", e => {
184 open = this.expand(wrap, content.dom)
185 window.addEventListener("mousedown", listeningOnClose = () => {
186 if (!isMenuEvent(wrap)) close()
191 function update(state) {
192 let inner = content.update(state)
193 wrap.style.display = inner ? "" : "none"
197 return {dom: wrap, update}
201 let menuDOM = crel("div", {class: prefix + "-dropdown-menu " + (this.options.class || "")}, items)
207 dom.removeChild(menuDOM)
210 dom.appendChild(menuDOM)
211 return {close, node: menuDOM}
215 function renderDropdownItems(items, view) {
216 let rendered = [], updates = []
217 for (let i = 0; i < items.length; i++) {
218 let {dom, update} = items[i].render(view)
219 rendered.push(crel("div", {class: prefix + "-dropdown-item"}, dom))
222 return {dom: rendered, update: combineUpdates(updates, rendered)}
225 function combineUpdates(updates, nodes) {
227 let something = false
228 for (let i = 0; i < updates.length; i++) {
229 let up = updates[i](state)
230 nodes[i].style.display = up ? "" : "none"
231 if (up) something = true
237 // ::- Represents a submenu wrapping a group of elements that start
238 // hidden and expand to the right when hovered over or tapped.
239 export class DropdownSubmenu {
240 // :: ([MenuElement], ?Object)
241 // Creates a submenu for the given group of menu elements. The
242 // following options are recognized:
244 // **`label`**`: string`
245 // : The label to show on the submenu.
246 constructor(content, options) {
247 this.options = options || {}
248 this.content = Array.isArray(content) ? content : [content]
251 // :: (EditorView) → {dom: dom.Node, update: (EditorState) → bool}
252 // Renders the submenu.
254 let items = renderDropdownItems(this.content, view)
256 let label = crel("div", {class: prefix + "-submenu-label"}, translate(view, this.options.label))
257 let wrap = crel("div", {class: prefix + "-submenu-wrap"}, label,
258 crel("div", {class: prefix + "-submenu"}, items.dom))
259 let listeningOnClose = null
260 label.addEventListener("mousedown", e => {
263 setClass(wrap, prefix + "-submenu-wrap-active")
264 if (!listeningOnClose)
265 window.addEventListener("mousedown", listeningOnClose = () => {
266 if (!isMenuEvent(wrap)) {
267 wrap.classList.remove(prefix + "-submenu-wrap-active")
268 window.removeEventListener("mousedown", listeningOnClose)
269 listeningOnClose = null
274 function update(state) {
275 let inner = items.update(state)
276 wrap.style.display = inner ? "" : "none"
279 return {dom: wrap, update}
283 // :: (EditorView, [[MenuElement]]) → {dom: dom.DocumentFragment, update: (EditorState) → bool}
284 // Render the given, possibly nested, array of menu elements into a
285 // document fragment, placing separators between them (and ensuring no
286 // superfluous separators appear when some of the groups turn out to
288 export function renderGrouped(view, content) {
289 let result = document.createDocumentFragment()
290 let updates = [], separators = []
291 for (let i = 0; i < content.length; i++) {
292 let items = content[i], localUpdates = [], localNodes = []
293 for (let j = 0; j < items.length; j++) {
294 let {dom, update} = items[j].render(view)
295 let span = crel("span", {class: prefix + "item"}, dom)
296 result.appendChild(span)
297 localNodes.push(span)
298 localUpdates.push(update)
300 if (localUpdates.length) {
301 updates.push(combineUpdates(localUpdates, localNodes))
302 if (i < content.length - 1)
303 separators.push(result.appendChild(separator()))
307 function update(state) {
308 let something = false, needSep = false
309 for (let i = 0; i < updates.length; i++) {
310 let hasContent = updates[i](state)
311 if (i) separators[i - 1].style.display = needSep && hasContent ? "" : "none"
313 if (hasContent) something = true
317 return {dom: result, update}
320 function separator() {
321 return crel("span", {class: prefix + "separator"})
327 // A set of basic editor-related icons. Contains the properties
328 // `join`, `lift`, `selectParentNode`, `undo`, `redo`, `strong`, `em`,
329 // `code`, `link`, `bulletList`, `orderedList`, and `blockquote`, each
330 // holding an object that can be used as the `icon` option to
332 export const icons = {
334 width: 800, height: 900,
335 path: "M0 75h800v125h-800z M0 825h800v-125h-800z M250 400h100v-100h100v100h100v100h-100v100h-100v-100h-100z"
338 width: 1024, height: 1024,
339 path: "M219 310v329q0 7-5 12t-12 5q-8 0-13-5l-164-164q-5-5-5-13t5-13l164-164q5-5 13-5 7 0 12 5t5 12zM1024 749v109q0 7-5 12t-12 5h-987q-7 0-12-5t-5-12v-109q0-7 5-12t12-5h987q7 0 12 5t5 12zM1024 530v109q0 7-5 12t-12 5h-621q-7 0-12-5t-5-12v-109q0-7 5-12t12-5h621q7 0 12 5t5 12zM1024 310v109q0 7-5 12t-12 5h-621q-7 0-12-5t-5-12v-109q0-7 5-12t12-5h621q7 0 12 5t5 12zM1024 91v109q0 7-5 12t-12 5h-987q-7 0-12-5t-5-12v-109q0-7 5-12t12-5h987q7 0 12 5t5 12z"
341 selectParentNode: {text: "\u2b1a", css: "font-weight: bold"},
343 width: 1024, height: 1024,
344 path: "M761 1024c113-206 132-520-313-509v253l-384-384 384-384v248c534-13 594 472 313 775z"
347 width: 1024, height: 1024,
348 path: "M576 248v-248l384 384-384 384v-253c-446-10-427 303-313 509-280-303-221-789 313-775z"
351 width: 805, height: 1024,
352 path: "M317 869q42 18 80 18 214 0 214-191 0-65-23-102-15-25-35-42t-38-26-46-14-48-6-54-1q-41 0-57 5 0 30-0 90t-0 90q0 4-0 38t-0 55 2 47 6 38zM309 442q24 4 62 4 46 0 81-7t62-25 42-51 14-81q0-40-16-70t-45-46-61-24-70-8q-28 0-74 7 0 28 2 86t2 86q0 15-0 45t-0 45q0 26 0 39zM0 950l1-53q8-2 48-9t60-15q4-6 7-15t4-19 3-18 1-21 0-19v-37q0-561-12-585-2-4-12-8t-25-6-28-4-27-2-17-1l-2-47q56-1 194-6t213-5q13 0 39 0t38 0q40 0 78 7t73 24 61 40 42 59 16 78q0 29-9 54t-22 41-36 32-41 25-48 22q88 20 146 76t58 141q0 57-20 102t-53 74-78 48-93 27-100 8q-25 0-75-1t-75-1q-60 0-175 6t-132 6z"
355 width: 585, height: 1024,
356 path: "M0 949l9-48q3-1 46-12t63-21q16-20 23-57 0-4 35-165t65-310 29-169v-14q-13-7-31-10t-39-4-33-3l10-58q18 1 68 3t85 4 68 1q27 0 56-1t69-4 56-3q-2 22-10 50-17 5-58 16t-62 19q-4 10-8 24t-5 22-4 26-3 24q-15 84-50 239t-44 203q-1 5-7 33t-11 51-9 47-3 32l0 10q9 2 105 17-1 25-9 56-6 0-18 0t-18 0q-16 0-49-5t-49-5q-78-1-117-1-29 0-81 5t-69 6z"
359 width: 896, height: 1024,
360 path: "M608 192l-96 96 224 224-224 224 96 96 288-320-288-320zM288 192l-288 320 288 320 96-96-224-224 224-224-96-96z"
363 width: 951, height: 1024,
364 path: "M832 694q0-22-16-38l-118-118q-16-16-38-16-24 0-41 18 1 1 10 10t12 12 8 10 7 14 2 15q0 22-16 38t-38 16q-8 0-15-2t-14-7-10-8-12-12-10-10q-18 17-18 41 0 22 16 38l117 118q15 15 38 15 22 0 38-14l84-83q16-16 16-38zM430 292q0-22-16-38l-117-118q-16-16-38-16-22 0-38 15l-84 83q-16 16-16 38 0 22 16 38l118 118q15 15 38 15 24 0 41-17-1-1-10-10t-12-12-8-10-7-14-2-15q0-22 16-38t38-16q8 0 15 2t14 7 10 8 12 12 10 10q18-17 18-41zM941 694q0 68-48 116l-84 83q-47 47-116 47-69 0-116-48l-117-118q-47-47-47-116 0-70 50-119l-50-50q-49 50-118 50-68 0-116-48l-118-118q-48-48-48-116t48-116l84-83q47-47 116-47 69 0 116 48l117 118q47 47 47 116 0 70-50 119l50 50q49-50 118-50 68 0 116 48l118 118q48 48 48 116z"
367 width: 768, height: 896,
368 path: "M0 512h128v-128h-128v128zM0 256h128v-128h-128v128zM0 768h128v-128h-128v128zM256 512h512v-128h-512v128zM256 256h512v-128h-512v128zM256 768h512v-128h-512v128z"
371 width: 768, height: 896,
372 path: "M320 512h448v-128h-448v128zM320 768h448v-128h-448v128zM320 128v128h448v-128h-448zM79 384h78v-256h-36l-85 23v50l43-2v185zM189 590c0-36-12-78-96-78-33 0-64 6-83 16l1 66c21-10 42-15 67-15s32 11 32 28c0 26-30 58-110 112v50h192v-67l-91 2c49-30 87-66 87-113l1-1z"
375 width: 640, height: 896,
376 path: "M0 448v256h256v-256h-128c0 0 0-128 128-128v-128c0 0-256 0-256 256zM640 320v-128c0 0-256 0-256 256v256h256v-256h-128c0 0 0-128 128-128z"
381 // Menu item for the `joinUp` command.
382 export const joinUpItem = new MenuItem({
383 title: "Join with above block",
385 select: state => joinUp(state),
390 // Menu item for the `lift` command.
391 export const liftItem = new MenuItem({
392 title: "Lift out of enclosing block",
394 select: state => lift(state),
399 // Menu item for the `selectParentNode` command.
400 export const selectParentNodeItem = new MenuItem({
401 title: "Select parent node",
402 run: selectParentNode,
403 select: state => selectParentNode(state),
404 icon: icons.selectParentNode
408 // Menu item for the `undo` command.
409 export let undoItem = new MenuItem({
410 title: "Undo last change",
412 enable: state => undo(state),
417 // Menu item for the `redo` command.
418 export let redoItem = new MenuItem({
419 title: "Redo last undone change",
421 enable: state => redo(state),
425 // :: (NodeType, Object) → MenuItem
426 // Build a menu item for wrapping the selection in a given node type.
427 // Adds `run` and `select` properties to the ones present in
428 // `options`. `options.attrs` may be an object or a function.
429 export function wrapItem(nodeType, options) {
430 let passedOptions = {
431 run(state, dispatch) {
432 // FIXME if (options.attrs instanceof Function) options.attrs(state, attrs => wrapIn(nodeType, attrs)(state))
433 return wrapIn(nodeType, options.attrs)(state, dispatch)
436 return wrapIn(nodeType, options.attrs instanceof Function ? null : options.attrs)(state)
439 for (let prop in options) passedOptions[prop] = options[prop]
440 return new MenuItem(passedOptions)
443 // :: (NodeType, Object) → MenuItem
444 // Build a menu item for changing the type of the textblock around the
445 // selection to the given type. Provides `run`, `active`, and `select`
446 // properties. Others must be given in `options`. `options.attrs` may
447 // be an object to provide the attributes for the textblock node.
448 export function blockTypeItem(nodeType, options) {
449 let command = setBlockType(nodeType, options.attrs)
450 let passedOptions = {
452 enable(state) { return command(state) },
454 let {$from, to, node} = state.selection
455 if (node) return node.hasMarkup(nodeType, options.attrs)
456 return to <= $from.end() && $from.parent.hasMarkup(nodeType, options.attrs)
459 for (let prop in options) passedOptions[prop] = options[prop]
460 return new MenuItem(passedOptions)
463 export function setAttrItem(attrName, attrValue, options) {
464 const command = setBlockAttr(attrName, attrValue);
465 const passedOptions = {
467 enable(state) { return command(state) },
469 const {$from, to, node} = state.selection
470 if (node) return node.attrs[attrValue] === attrValue;
471 return to <= $from.end() && $from.parent.attrs[attrValue] === attrValue;
474 for (const prop in options) passedOptions[prop] = options[prop]
475 return new MenuItem(passedOptions)
478 // Work around classList.toggle being broken in IE11
479 function setClass(dom, cls, on) {
480 if (on) dom.classList.add(cls)
481 else dom.classList.remove(cls)