]> BookStack Code Mirror - bookstack/blob - resources/js/editor/menu/icons.js
Added in a custom menubar
[bookstack] / resources / js / editor / menu / icons.js
1 /**
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
6  */
7
8 const SVG = "http://www.w3.org/2000/svg"
9 const XLINK = "http://www.w3.org/1999/xlink"
10
11 const prefix = "ProseMirror-icon"
12
13 function hashPath(path) {
14   let hash = 0
15   for (let i = 0; i < path.length; i++)
16     hash = (((hash << 5) - hash) + path.charCodeAt(i)) | 0
17   return hash
18 }
19
20 export function getIcon(icon) {
21   let node = document.createElement("div")
22   node.className = prefix
23   if (icon.path) {
24     let name = "pm-icon-" + hashPath(icon.path).toString(16)
25     if (!document.getElementById(name)) buildSVG(name, icon)
26     let svg = node.appendChild(document.createElementNS(SVG, "svg"))
27     svg.style.width = (icon.width / icon.height) + "em"
28     let use = svg.appendChild(document.createElementNS(SVG, "use"))
29     use.setAttributeNS(XLINK, "href", /([^#]*)/.exec(document.location)[1] + "#" + name)
30   } else if (icon.dom) {
31     node.appendChild(icon.dom.cloneNode(true))
32   } else {
33     node.appendChild(document.createElement("span")).textContent = icon.text || ''
34     if (icon.css) node.firstChild.style.cssText = icon.css
35   }
36   return node
37 }
38
39 function buildSVG(name, data) {
40   let collection = document.getElementById(prefix + "-collection")
41   if (!collection) {
42     collection = document.createElementNS(SVG, "svg")
43     collection.id = prefix + "-collection"
44     collection.style.display = "none"
45     document.body.insertBefore(collection, document.body.firstChild)
46   }
47   let sym = document.createElementNS(SVG, "symbol")
48   sym.id = name
49   sym.setAttribute("viewBox", "0 0 " + data.width + " " + data.height)
50   let path = sym.appendChild(document.createElementNS(SVG, "path"))
51   path.setAttribute("d", data.path)
52   collection.appendChild(sym)
53 }