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 const SVG = "http://www.w3.org/2000/svg"
9 const XLINK = "http://www.w3.org/1999/xlink"
11 const prefix = "ProseMirror-icon"
13 function hashPath(path) {
15 for (let i = 0; i < path.length; i++)
16 hash = (((hash << 5) - hash) + path.charCodeAt(i)) | 0
20 export function getIcon(icon) {
21 let node = document.createElement("div")
22 node.className = prefix
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))
33 node.appendChild(document.createElement("span")).textContent = icon.text || ''
34 if (icon.css) node.firstChild.style.cssText = icon.css
39 function buildSVG(name, data) {
40 let collection = document.getElementById(prefix + "-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)
47 let sym = document.createElementNS(SVG, "symbol")
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)