prosemirror-menu 1.2.2 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 1.2.4 (2023-08-20)
2
+
3
+ ### Bug fixes
4
+
5
+ Fix a bug where icon creation crashed because it couldn't find a Document value.
6
+
7
+ ## 1.2.3 (2023-08-16)
8
+
9
+ ### Bug fixes
10
+
11
+ Don't directly use the global `window`/`document`, to fix use in a different frame or shadow root.
12
+
1
13
  ## 1.2.2 (2023-05-17)
2
14
 
3
15
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -6,11 +6,17 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
6
6
 
7
7
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
8
8
 
9
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
9
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
10
10
 
11
- Object.defineProperty(exports, '__esModule', {
12
- value: true
13
- });
11
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
12
+
13
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
14
+
15
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
16
+
17
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
18
+
19
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
14
20
 
15
21
  var crel = require('crelt');
16
22
 
@@ -20,14 +26,6 @@ var prosemirrorHistory = require('prosemirror-history');
20
26
 
21
27
  var prosemirrorState = require('prosemirror-state');
22
28
 
23
- function _interopDefaultLegacy(e) {
24
- return e && _typeof(e) === 'object' && 'default' in e ? e : {
25
- 'default': e
26
- };
27
- }
28
-
29
- var crel__default = _interopDefaultLegacy(crel);
30
-
31
29
  var SVG = "http://www.w3.org/2000/svg";
32
30
  var XLINK = "http://www.w3.org/1999/xlink";
33
31
  var prefix$2 = "ProseMirror-icon";
@@ -42,8 +40,9 @@ function hashPath(path) {
42
40
  return hash;
43
41
  }
44
42
 
45
- function getIcon(icon) {
46
- var node = document.createElement("div");
43
+ function getIcon(root, icon) {
44
+ var doc = (root.nodeType == 9 ? root : root.ownerDocument) || document;
45
+ var node = doc.createElement("div");
47
46
  node.className = prefix$2;
48
47
 
49
48
  if (icon.path) {
@@ -51,37 +50,42 @@ function getIcon(icon) {
51
50
  width = icon.width,
52
51
  height = icon.height;
53
52
  var name = "pm-icon-" + hashPath(path).toString(16);
54
- if (!document.getElementById(name)) buildSVG(name, icon);
55
- var svg = node.appendChild(document.createElementNS(SVG, "svg"));
53
+ if (!doc.getElementById(name)) buildSVG(root, name, icon);
54
+ var svg = node.appendChild(doc.createElementNS(SVG, "svg"));
56
55
  svg.style.width = width / height + "em";
57
- var use = svg.appendChild(document.createElementNS(SVG, "use"));
58
- use.setAttributeNS(XLINK, "href", /([^#]*)/.exec(document.location.toString())[1] + "#" + name);
56
+ var use = svg.appendChild(doc.createElementNS(SVG, "use"));
57
+ use.setAttributeNS(XLINK, "href", /([^#]*)/.exec(doc.location.toString())[1] + "#" + name);
59
58
  } else if (icon.dom) {
60
59
  node.appendChild(icon.dom.cloneNode(true));
61
60
  } else {
62
61
  var text = icon.text,
63
62
  css = icon.css;
64
- node.appendChild(document.createElement("span")).textContent = text || '';
63
+ node.appendChild(doc.createElement("span")).textContent = text || '';
65
64
  if (css) node.firstChild.style.cssText = css;
66
65
  }
67
66
 
68
67
  return node;
69
68
  }
70
69
 
71
- function buildSVG(name, data) {
72
- var collection = document.getElementById(prefix$2 + "-collection");
70
+ function buildSVG(root, name, data) {
71
+ var _ref = root.nodeType == 9 ? [root, root.body] : [root.ownerDocument || document, root],
72
+ _ref2 = _slicedToArray(_ref, 2),
73
+ doc = _ref2[0],
74
+ top = _ref2[1];
75
+
76
+ var collection = doc.getElementById(prefix$2 + "-collection");
73
77
 
74
78
  if (!collection) {
75
- collection = document.createElementNS(SVG, "svg");
79
+ collection = doc.createElementNS(SVG, "svg");
76
80
  collection.id = prefix$2 + "-collection";
77
81
  collection.style.display = "none";
78
- document.body.insertBefore(collection, document.body.firstChild);
82
+ top.insertBefore(collection, top.firstChild);
79
83
  }
80
84
 
81
- var sym = document.createElementNS(SVG, "symbol");
85
+ var sym = doc.createElementNS(SVG, "symbol");
82
86
  sym.id = name;
83
87
  sym.setAttribute("viewBox", "0 0 " + data.width + " " + data.height);
84
- var path = sym.appendChild(document.createElementNS(SVG, "path"));
88
+ var path = sym.appendChild(doc.createElementNS(SVG, "path"));
85
89
  path.setAttribute("d", data.path);
86
90
  collection.appendChild(sym);
87
91
  }
@@ -99,7 +103,7 @@ var MenuItem = function () {
99
103
  key: "render",
100
104
  value: function render(view) {
101
105
  var spec = this.spec;
102
- var dom = spec.render ? spec.render(view) : spec.icon ? getIcon(spec.icon) : spec.label ? crel__default["default"]("div", null, translate(view, spec.label)) : null;
106
+ var dom = spec.render ? spec.render(view) : spec.icon ? getIcon(view.root, spec.icon) : spec.label ? crel("div", null, translate(view, spec.label)) : null;
103
107
  if (!dom) throw new RangeError("MenuItem without icon or label property");
104
108
 
105
109
  if (spec.title) {
@@ -181,12 +185,13 @@ var Dropdown = function () {
181
185
  var _this = this;
182
186
 
183
187
  var content = renderDropdownItems(this.content, view);
184
- var label = crel__default["default"]("div", {
188
+ var win = view.dom.ownerDocument.defaultView || window;
189
+ var label = crel("div", {
185
190
  "class": prefix$1 + "-dropdown " + (this.options["class"] || ""),
186
191
  style: this.options.css
187
192
  }, translate(view, this.options.label || ""));
188
193
  if (this.options.title) label.setAttribute("title", translate(view, this.options.title));
189
- var wrap = crel__default["default"]("div", {
194
+ var wrap = crel("div", {
190
195
  "class": prefix$1 + "-dropdown-wrap"
191
196
  }, label);
192
197
  var open = null;
@@ -195,7 +200,7 @@ var Dropdown = function () {
195
200
  var close = function close() {
196
201
  if (open && open.close()) {
197
202
  open = null;
198
- window.removeEventListener("mousedown", listeningOnClose);
203
+ win.removeEventListener("mousedown", listeningOnClose);
199
204
  }
200
205
  };
201
206
 
@@ -207,7 +212,7 @@ var Dropdown = function () {
207
212
  close();
208
213
  } else {
209
214
  open = _this.expand(wrap, content.dom);
210
- window.addEventListener("mousedown", listeningOnClose = function listeningOnClose() {
215
+ win.addEventListener("mousedown", listeningOnClose = function listeningOnClose() {
211
216
  if (!isMenuEvent(wrap)) close();
212
217
  });
213
218
  }
@@ -227,7 +232,7 @@ var Dropdown = function () {
227
232
  }, {
228
233
  key: "expand",
229
234
  value: function expand(dom, items) {
230
- var menuDOM = crel__default["default"]("div", {
235
+ var menuDOM = crel("div", {
231
236
  "class": prefix$1 + "-dropdown-menu " + (this.options["class"] || "")
232
237
  }, items);
233
238
  var done = false;
@@ -259,7 +264,7 @@ function renderDropdownItems(items, view) {
259
264
  dom = _items$i$render.dom,
260
265
  update = _items$i$render.update;
261
266
 
262
- rendered.push(crel__default["default"]("div", {
267
+ rendered.push(crel("div", {
263
268
  "class": prefix$1 + "-dropdown-item"
264
269
  }, dom));
265
270
  updates.push(update);
@@ -299,12 +304,13 @@ var DropdownSubmenu = function () {
299
304
  key: "render",
300
305
  value: function render(view) {
301
306
  var items = renderDropdownItems(this.content, view);
302
- var label = crel__default["default"]("div", {
307
+ var win = view.dom.ownerDocument.defaultView || window;
308
+ var label = crel("div", {
303
309
  "class": prefix$1 + "-submenu-label"
304
310
  }, translate(view, this.options.label || ""));
305
- var wrap = crel__default["default"]("div", {
311
+ var wrap = crel("div", {
306
312
  "class": prefix$1 + "-submenu-wrap"
307
- }, label, crel__default["default"]("div", {
313
+ }, label, crel("div", {
308
314
  "class": prefix$1 + "-submenu"
309
315
  }, items.dom));
310
316
  var _listeningOnClose = null;
@@ -312,10 +318,10 @@ var DropdownSubmenu = function () {
312
318
  e.preventDefault();
313
319
  markMenuEvent(e);
314
320
  setClass(wrap, prefix$1 + "-submenu-wrap-active", false);
315
- if (!_listeningOnClose) window.addEventListener("mousedown", _listeningOnClose = function listeningOnClose() {
321
+ if (!_listeningOnClose) win.addEventListener("mousedown", _listeningOnClose = function listeningOnClose() {
316
322
  if (!isMenuEvent(wrap)) {
317
323
  wrap.classList.remove(prefix$1 + "-submenu-wrap-active");
318
- window.removeEventListener("mousedown", _listeningOnClose);
324
+ win.removeEventListener("mousedown", _listeningOnClose);
319
325
  _listeningOnClose = null;
320
326
  }
321
327
  });
@@ -352,7 +358,7 @@ function renderGrouped(view, content) {
352
358
  dom = _items$j$render.dom,
353
359
  _update = _items$j$render.update;
354
360
 
355
- var span = crel__default["default"]("span", {
361
+ var span = crel("span", {
356
362
  "class": prefix$1 + "item"
357
363
  }, dom);
358
364
  result.appendChild(span);
@@ -370,10 +376,10 @@ function renderGrouped(view, content) {
370
376
  var something = false,
371
377
  needSep = false;
372
378
 
373
- for (var _i = 0; _i < updates.length; _i++) {
374
- var hasContent = updates[_i](state);
379
+ for (var _i2 = 0; _i2 < updates.length; _i2++) {
380
+ var hasContent = updates[_i2](state);
375
381
 
376
- if (_i) separators[_i - 1].style.display = needSep && hasContent ? "" : "none";
382
+ if (_i2) separators[_i2 - 1].style.display = needSep && hasContent ? "" : "none";
377
383
  needSep = hasContent;
378
384
  if (hasContent) something = true;
379
385
  }
@@ -388,7 +394,7 @@ function renderGrouped(view, content) {
388
394
  }
389
395
 
390
396
  function separator() {
391
- return crel__default["default"]("span", {
397
+ return crel("span", {
392
398
  "class": prefix$1 + "separator"
393
399
  });
394
400
  }
@@ -569,10 +575,10 @@ var MenuBarView = function () {
569
575
  this.widthForMaxHeight = 0;
570
576
  this.floating = false;
571
577
  this.scrollHandler = null;
572
- this.wrapper = crel__default["default"]("div", {
578
+ this.wrapper = crel("div", {
573
579
  "class": prefix + "-wrapper"
574
580
  });
575
- this.menu = this.wrapper.appendChild(crel__default["default"]("div", {
581
+ this.menu = this.wrapper.appendChild(crel("div", {
576
582
  "class": prefix
577
583
  }));
578
584
  this.menu.className = prefix;
@@ -655,7 +661,7 @@ var MenuBarView = function () {
655
661
  } else {
656
662
  var border = (parent.offsetWidth - parent.clientWidth) / 2;
657
663
  this.menu.style.left = editorRect.left + border + "px";
658
- this.menu.style.display = editorRect.top > window.innerHeight ? "none" : "";
664
+ this.menu.style.display = editorRect.top > (this.editorView.dom.ownerDocument.defaultView || window).innerHeight ? "none" : "";
659
665
  if (scrollAncestor) this.menu.style.top = top + "px";
660
666
  }
661
667
  } else {
@@ -666,7 +672,7 @@ var MenuBarView = function () {
666
672
  this.menu.style.width = menuRect.width + "px";
667
673
  if (scrollAncestor) this.menu.style.top = top + "px";
668
674
  this.menu.style.position = "fixed";
669
- this.spacer = crel__default["default"]("div", {
675
+ this.spacer = crel("div", {
670
676
  "class": prefix + "-spacer",
671
677
  style: "height: ".concat(menuRect.height, "px")
672
678
  });
@@ -696,7 +702,7 @@ function findWrappingScrollable(node) {
696
702
  }
697
703
 
698
704
  function getAllWrapping(node) {
699
- var res = [window];
705
+ var res = [node.ownerDocument.defaultView || window];
700
706
 
701
707
  for (var cur = node.parentNode; cur; cur = cur.parentNode) {
702
708
  res.push(cur);
package/dist/index.d.cts CHANGED
@@ -55,7 +55,7 @@ specifying a string of text that makes up the icon, with an
55
55
  optional `css` property giving additional CSS styling for the
56
56
  text. _Or_ it may contain `dom` property containing a DOM node.
57
57
  */
58
- declare type IconSpec = {
58
+ type IconSpec = {
59
59
  path: string;
60
60
  width: number;
61
61
  height: number;
package/dist/index.d.ts CHANGED
@@ -55,7 +55,7 @@ specifying a string of text that makes up the icon, with an
55
55
  optional `css` property giving additional CSS styling for the
56
56
  text. _Or_ it may contain `dom` property containing a DOM node.
57
57
  */
58
- declare type IconSpec = {
58
+ type IconSpec = {
59
59
  path: string;
60
60
  width: number;
61
61
  height: number;
package/dist/index.js CHANGED
@@ -12,42 +12,44 @@ function hashPath(path) {
12
12
  hash = (((hash << 5) - hash) + path.charCodeAt(i)) | 0;
13
13
  return hash;
14
14
  }
15
- function getIcon(icon) {
16
- let node = document.createElement("div");
15
+ function getIcon(root, icon) {
16
+ let doc = (root.nodeType == 9 ? root : root.ownerDocument) || document;
17
+ let node = doc.createElement("div");
17
18
  node.className = prefix$2;
18
19
  if (icon.path) {
19
20
  let { path, width, height } = icon;
20
21
  let name = "pm-icon-" + hashPath(path).toString(16);
21
- if (!document.getElementById(name))
22
- buildSVG(name, icon);
23
- let svg = node.appendChild(document.createElementNS(SVG, "svg"));
22
+ if (!doc.getElementById(name))
23
+ buildSVG(root, name, icon);
24
+ let svg = node.appendChild(doc.createElementNS(SVG, "svg"));
24
25
  svg.style.width = (width / height) + "em";
25
- let use = svg.appendChild(document.createElementNS(SVG, "use"));
26
- use.setAttributeNS(XLINK, "href", /([^#]*)/.exec(document.location.toString())[1] + "#" + name);
26
+ let use = svg.appendChild(doc.createElementNS(SVG, "use"));
27
+ use.setAttributeNS(XLINK, "href", /([^#]*)/.exec(doc.location.toString())[1] + "#" + name);
27
28
  }
28
29
  else if (icon.dom) {
29
30
  node.appendChild(icon.dom.cloneNode(true));
30
31
  }
31
32
  else {
32
33
  let { text, css } = icon;
33
- node.appendChild(document.createElement("span")).textContent = text || '';
34
+ node.appendChild(doc.createElement("span")).textContent = text || '';
34
35
  if (css)
35
36
  node.firstChild.style.cssText = css;
36
37
  }
37
38
  return node;
38
39
  }
39
- function buildSVG(name, data) {
40
- let collection = document.getElementById(prefix$2 + "-collection");
40
+ function buildSVG(root, name, data) {
41
+ let [doc, top] = root.nodeType == 9 ? [root, root.body] : [root.ownerDocument || document, root];
42
+ let collection = doc.getElementById(prefix$2 + "-collection");
41
43
  if (!collection) {
42
- collection = document.createElementNS(SVG, "svg");
44
+ collection = doc.createElementNS(SVG, "svg");
43
45
  collection.id = prefix$2 + "-collection";
44
46
  collection.style.display = "none";
45
- document.body.insertBefore(collection, document.body.firstChild);
47
+ top.insertBefore(collection, top.firstChild);
46
48
  }
47
- let sym = document.createElementNS(SVG, "symbol");
49
+ let sym = doc.createElementNS(SVG, "symbol");
48
50
  sym.id = name;
49
51
  sym.setAttribute("viewBox", "0 0 " + data.width + " " + data.height);
50
- let path = sym.appendChild(document.createElementNS(SVG, "path"));
52
+ let path = sym.appendChild(doc.createElementNS(SVG, "path"));
51
53
  path.setAttribute("d", data.path);
52
54
  collection.appendChild(sym);
53
55
  }
@@ -75,7 +77,7 @@ class MenuItem {
75
77
  render(view) {
76
78
  let spec = this.spec;
77
79
  let dom = spec.render ? spec.render(view)
78
- : spec.icon ? getIcon(spec.icon)
80
+ : spec.icon ? getIcon(view.root, spec.icon)
79
81
  : spec.label ? crel("div", null, translate(view, spec.label))
80
82
  : null;
81
83
  if (!dom)
@@ -148,6 +150,7 @@ class Dropdown {
148
150
  */
149
151
  render(view) {
150
152
  let content = renderDropdownItems(this.content, view);
153
+ let win = view.dom.ownerDocument.defaultView || window;
151
154
  let label = crel("div", { class: prefix$1 + "-dropdown " + (this.options.class || ""),
152
155
  style: this.options.css }, translate(view, this.options.label || ""));
153
156
  if (this.options.title)
@@ -158,7 +161,7 @@ class Dropdown {
158
161
  let close = () => {
159
162
  if (open && open.close()) {
160
163
  open = null;
161
- window.removeEventListener("mousedown", listeningOnClose);
164
+ win.removeEventListener("mousedown", listeningOnClose);
162
165
  }
163
166
  };
164
167
  label.addEventListener("mousedown", e => {
@@ -169,7 +172,7 @@ class Dropdown {
169
172
  }
170
173
  else {
171
174
  open = this.expand(wrap, content.dom);
172
- window.addEventListener("mousedown", listeningOnClose = () => {
175
+ win.addEventListener("mousedown", listeningOnClose = () => {
173
176
  if (!isMenuEvent(wrap))
174
177
  close();
175
178
  });
@@ -242,6 +245,7 @@ class DropdownSubmenu {
242
245
  */
243
246
  render(view) {
244
247
  let items = renderDropdownItems(this.content, view);
248
+ let win = view.dom.ownerDocument.defaultView || window;
245
249
  let label = crel("div", { class: prefix$1 + "-submenu-label" }, translate(view, this.options.label || ""));
246
250
  let wrap = crel("div", { class: prefix$1 + "-submenu-wrap" }, label, crel("div", { class: prefix$1 + "-submenu" }, items.dom));
247
251
  let listeningOnClose = null;
@@ -250,10 +254,10 @@ class DropdownSubmenu {
250
254
  markMenuEvent(e);
251
255
  setClass(wrap, prefix$1 + "-submenu-wrap-active", false);
252
256
  if (!listeningOnClose)
253
- window.addEventListener("mousedown", listeningOnClose = () => {
257
+ win.addEventListener("mousedown", listeningOnClose = () => {
254
258
  if (!isMenuEvent(wrap)) {
255
259
  wrap.classList.remove(prefix$1 + "-submenu-wrap-active");
256
- window.removeEventListener("mousedown", listeningOnClose);
260
+ win.removeEventListener("mousedown", listeningOnClose);
257
261
  listeningOnClose = null;
258
262
  }
259
263
  });
@@ -547,7 +551,8 @@ class MenuBarView {
547
551
  else {
548
552
  let border = (parent.offsetWidth - parent.clientWidth) / 2;
549
553
  this.menu.style.left = (editorRect.left + border) + "px";
550
- this.menu.style.display = (editorRect.top > window.innerHeight ? "none" : "");
554
+ this.menu.style.display = editorRect.top > (this.editorView.dom.ownerDocument.defaultView || window).innerHeight
555
+ ? "none" : "";
551
556
  if (scrollAncestor)
552
557
  this.menu.style.top = top + "px";
553
558
  }
@@ -583,7 +588,7 @@ function findWrappingScrollable(node) {
583
588
  return cur;
584
589
  }
585
590
  function getAllWrapping(node) {
586
- let res = [window];
591
+ let res = [node.ownerDocument.defaultView || window];
587
592
  for (let cur = node.parentNode; cur; cur = cur.parentNode)
588
593
  res.push(cur);
589
594
  return res;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prosemirror-menu",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Simple menu elements for ProseMirror",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
package/src/icons.ts CHANGED
@@ -11,40 +11,43 @@ function hashPath(path: string) {
11
11
  }
12
12
 
13
13
  export function getIcon(
14
+ root: Document | ShadowRoot,
14
15
  icon: {path: string, width: number, height: number} | {text: string, css?: string} | {dom: Node}
15
16
  ): HTMLElement {
16
- let node = document.createElement("div")
17
+ let doc = (root.nodeType == 9 ? root as Document : root.ownerDocument) || document
18
+ let node = doc.createElement("div")
17
19
  node.className = prefix
18
20
  if ((icon as any).path) {
19
21
  let {path, width, height} = icon as {path: string, width: number, height: number}
20
22
  let name = "pm-icon-" + hashPath(path).toString(16)
21
- if (!document.getElementById(name)) buildSVG(name, icon as {path: string, width: number, height: number})
22
- let svg = node.appendChild(document.createElementNS(SVG, "svg"))
23
+ if (!doc.getElementById(name)) buildSVG(root, name, icon as {path: string, width: number, height: number})
24
+ let svg = node.appendChild(doc.createElementNS(SVG, "svg"))
23
25
  svg.style.width = (width / height) + "em"
24
- let use = svg.appendChild(document.createElementNS(SVG, "use"))
25
- use.setAttributeNS(XLINK, "href", /([^#]*)/.exec(document.location.toString())![1] + "#" + name)
26
+ let use = svg.appendChild(doc.createElementNS(SVG, "use"))
27
+ use.setAttributeNS(XLINK, "href", /([^#]*)/.exec(doc.location.toString())![1] + "#" + name)
26
28
  } else if ((icon as any).dom) {
27
29
  node.appendChild((icon as any).dom.cloneNode(true))
28
30
  } else {
29
31
  let {text, css} = icon as {text: string, css?: string}
30
- node.appendChild(document.createElement("span")).textContent = text || ''
32
+ node.appendChild(doc.createElement("span")).textContent = text || ''
31
33
  if (css) (node.firstChild as HTMLElement).style.cssText = css
32
34
  }
33
35
  return node
34
36
  }
35
37
 
36
- function buildSVG(name: string, data: {width: number, height: number, path: string}) {
37
- let collection = document.getElementById(prefix + "-collection") as Element
38
+ function buildSVG(root: Document | ShadowRoot, name: string, data: {width: number, height: number, path: string}) {
39
+ let [doc, top] = root.nodeType == 9 ? [root as Document, (root as Document).body] : [root.ownerDocument || document, root]
40
+ let collection = doc.getElementById(prefix + "-collection") as Element
38
41
  if (!collection) {
39
- collection = document.createElementNS(SVG, "svg")
42
+ collection = doc.createElementNS(SVG, "svg")
40
43
  collection.id = prefix + "-collection"
41
44
  ;(collection as HTMLElement).style.display = "none"
42
- document.body.insertBefore(collection, document.body.firstChild)
45
+ top.insertBefore(collection, top.firstChild)
43
46
  }
44
- let sym = document.createElementNS(SVG, "symbol")
47
+ let sym = doc.createElementNS(SVG, "symbol")
45
48
  sym.id = name
46
49
  sym.setAttribute("viewBox", "0 0 " + data.width + " " + data.height)
47
- let path = sym.appendChild(document.createElementNS(SVG, "path"))
50
+ let path = sym.appendChild(doc.createElementNS(SVG, "path"))
48
51
  path.setAttribute("d", data.path)
49
52
  collection.appendChild(sym)
50
53
  }
package/src/menu.ts CHANGED
@@ -34,7 +34,7 @@ export class MenuItem implements MenuElement {
34
34
  render(view: EditorView) {
35
35
  let spec = this.spec
36
36
  let dom = spec.render ? spec.render(view)
37
- : spec.icon ? getIcon(spec.icon)
37
+ : spec.icon ? getIcon(view.root, spec.icon)
38
38
  : spec.label ? crel("div", null, translate(view, spec.label))
39
39
  : null
40
40
  if (!dom) throw new RangeError("MenuItem without icon or label property")
@@ -171,6 +171,7 @@ export class Dropdown implements MenuElement {
171
171
  /// Render the dropdown menu and sub-items.
172
172
  render(view: EditorView) {
173
173
  let content = renderDropdownItems(this.content, view)
174
+ let win = view.dom.ownerDocument.defaultView || window
174
175
 
175
176
  let label = crel("div", {class: prefix + "-dropdown " + (this.options.class || ""),
176
177
  style: this.options.css},
@@ -182,7 +183,7 @@ export class Dropdown implements MenuElement {
182
183
  let close = () => {
183
184
  if (open && open.close()) {
184
185
  open = null
185
- window.removeEventListener("mousedown", listeningOnClose!)
186
+ win.removeEventListener("mousedown", listeningOnClose!)
186
187
  }
187
188
  }
188
189
  label.addEventListener("mousedown", e => {
@@ -192,7 +193,7 @@ export class Dropdown implements MenuElement {
192
193
  close()
193
194
  } else {
194
195
  open = this.expand(wrap, content.dom)
195
- window.addEventListener("mousedown", listeningOnClose = () => {
196
+ win.addEventListener("mousedown", listeningOnClose = () => {
196
197
  if (!isMenuEvent(wrap)) close()
197
198
  })
198
199
  }
@@ -270,6 +271,7 @@ export class DropdownSubmenu implements MenuElement {
270
271
  /// Renders the submenu.
271
272
  render(view: EditorView) {
272
273
  let items = renderDropdownItems(this.content, view)
274
+ let win = view.dom.ownerDocument.defaultView || window
273
275
 
274
276
  let label = crel("div", {class: prefix + "-submenu-label"}, translate(view, this.options.label || ""))
275
277
  let wrap = crel("div", {class: prefix + "-submenu-wrap"}, label,
@@ -280,10 +282,10 @@ export class DropdownSubmenu implements MenuElement {
280
282
  markMenuEvent(e)
281
283
  setClass(wrap, prefix + "-submenu-wrap-active", false)
282
284
  if (!listeningOnClose)
283
- window.addEventListener("mousedown", listeningOnClose = () => {
285
+ win.addEventListener("mousedown", listeningOnClose = () => {
284
286
  if (!isMenuEvent(wrap)) {
285
287
  wrap.classList.remove(prefix + "-submenu-wrap-active")
286
- window.removeEventListener("mousedown", listeningOnClose!)
288
+ win.removeEventListener("mousedown", listeningOnClose!)
287
289
  listeningOnClose = null
288
290
  }
289
291
  })
package/src/menubar.ts CHANGED
@@ -114,7 +114,8 @@ class MenuBarView {
114
114
  } else {
115
115
  let border = (parent.offsetWidth - parent.clientWidth) / 2
116
116
  this.menu.style.left = (editorRect.left + border) + "px"
117
- this.menu.style.display = (editorRect.top > window.innerHeight ? "none" : "")
117
+ this.menu.style.display = editorRect.top > (this.editorView.dom.ownerDocument.defaultView || window).innerHeight
118
+ ? "none" : ""
118
119
  if (scrollAncestor) this.menu.style.top = top + "px"
119
120
  }
120
121
  } else {
@@ -149,7 +150,7 @@ function findWrappingScrollable(node: Node) {
149
150
  }
150
151
 
151
152
  function getAllWrapping(node: Node) {
152
- let res: (Node | Window)[] = [window]
153
+ let res: (Node | Window)[] = [node.ownerDocument!.defaultView || window]
153
154
  for (let cur = node.parentNode; cur; cur = cur.parentNode)
154
155
  res.push(cur)
155
156
  return res