@themarioga/grid-editor 9.1.0 → 9.2.0

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/docs/plugins.md CHANGED
@@ -192,7 +192,7 @@ element, and where there may be several, an array.
192
192
  | `ge.confirm(message, options, answer)` | A question in the editor's confirm modal, the one deleting asks in. `options`: `title`, `ok`, `cancel` - the texts, the locale's by default - and `danger`, a red ok button. `answer(true)` for ok, `answer(false)` for cancel, the close button or Escape, `answer(null)` when the modal is taken away unanswered: `setLocale`, `destroy`, or another question. The browser's `confirm` without Bootstrap's javascript |
193
193
  | `ge.refreshToolbar()` | The toolbar built again, from what the feature plugins' `toolbar`s hold now: for a plugin whose buttons come and go. The category shown with `toolbar_groups`, the preview and the source view stay as they are. Does nothing before the toolbar is first built, and after `destroy` |
194
194
  | `ge.getActiveTarget()` | The node the toolbar adds to and the sidebar shows - any node with settings - as the public method gives it: `null` without `active_target` |
195
- | `ge.placeInTarget(node, kind, source)` | Add a node you made to the active target as the toolbar adds what it makes: at the end of what the target holds, in the row or column it lacks, a section on the canvas after the block the target is in. The add events fire with `kind` and `source`. The node added, or `null` with no target or when a handler canceled |
195
+ | `ge.placeInTarget(node, kind, source, options)` | Add a node you made to the active target as the toolbar adds what it makes: at the end of what the target holds, in the row or column it lacks, a section on the canvas after the block the target is in. With no target it adds nothing, unless `options.fallback` is `'canvas'`: then it goes at the end of the canvas, in the row and column it lacks. The add events fire with `kind` and `source`. The node added, or `null` when nothing was added or a handler canceled |
196
196
  | `ge.removeNode(node, { source, confirm, before })` | Remove a node as its own delete tool does - a text's editor closes first, each kind goes out its way - as asked: `source` goes in the `before-delete` and `after-delete` payloads (default `'tool'`), `confirm: false` skips the question, and `before()` is called once `before-delete` has let it through, before the node goes. `true` if it went ahead, `false` for a node with no delete tool or one a handler turned away |
197
197
  | `ge.setActiveTarget(node)` | Choose the node the toolbar adds to, or `null` for none: the public method without its warnings. `true` when it took, `false` for a node that is not one with settings, or without `active_target`. A plugin that puts the choice back after it remade the canvas does not know the node is still one |
198
198
  | `ge.panelOpen()` | Whether a settings panel is open in a popover, an offcanvas or a modal, or one is unfolded inline in a drawer, or the editor's dialog or delete confirmation is up. The sidebar does not count: it is always there |
package/package.json CHANGED
@@ -32,7 +32,7 @@
32
32
  "publishConfig": {
33
33
  "access": "public"
34
34
  },
35
- "version": "9.1.0",
35
+ "version": "9.2.0",
36
36
  "main": "dist/grideditor.js",
37
37
  "module": "dist/grideditor.esm.js",
38
38
  "types": "dist/grideditor.d.ts",
@@ -1138,8 +1138,21 @@ function build(instance, baseElem, optionsOrMethod) {
1138
1138
 
1139
1139
  var signal = { signal: lifetime.signal };
1140
1140
 
1141
- // Make controls fixed on scroll
1142
- window.addEventListener('scroll', onScroll, signal);
1141
+ // Make controls fixed on scroll. Once a frame, after the
1142
+ // layout: a scroll also comes when the page changes height - a
1143
+ // getHtml takes every drawer off and puts it back - and read
1144
+ // there and then, the toolbar's position laid the page out again
1145
+ var scrollFrame = null;
1146
+ window.addEventListener('scroll', function() {
1147
+ if (scrollFrame !== null) { return; }
1148
+ scrollFrame = window.requestAnimationFrame(function() {
1149
+ scrollFrame = null;
1150
+ if (!destroyed && mainControls) { onScroll(); }
1151
+ });
1152
+ }, signal);
1153
+ lifetime.signal.addEventListener('abort', function() {
1154
+ if (scrollFrame !== null) { window.cancelAnimationFrame(scrollFrame); }
1155
+ });
1143
1156
 
1144
1157
  /* A click on the host's plain content offers to make it a text */
1145
1158
  dom.delegate(canvas, 'click', '.ge-content', onContentClick, signal);
@@ -1889,13 +1902,16 @@ function build(instance, baseElem, optionsOrMethod) {
1889
1902
 
1890
1903
  /**
1891
1904
  * A node already made - a paste, say - added to the active target,
1892
- * as the toolbar's buttons add what they make. Null with no target,
1893
- * or when a handler canceled the add.
1905
+ * as the toolbar's buttons add what they make. With no target, at
1906
+ * the end of the canvas when `options.fallback` is 'canvas', in the
1907
+ * row and column it lacks; otherwise null, as when a handler
1908
+ * canceled the add.
1894
1909
  */
1895
- function placeInTarget(node, kind, source) {
1896
- if (!activeTarget) { return null; }
1910
+ function placeInTarget(node, kind, source, options) {
1911
+ var toCanvas = !activeTarget && (options || {}).fallback === 'canvas';
1912
+ if (!activeTarget && !toCanvas) { return null; }
1897
1913
 
1898
- var where = { region: effectiveRegion(activeTarget), before: null };
1914
+ var where = { region: toCanvas ? canvas : effectiveRegion(activeTarget), before: null };
1899
1915
  var placed = wrapFor(where.region, node);
1900
1916
 
1901
1917
  // Wrapped in a column, a block still has to be one the column
@@ -2505,26 +2521,35 @@ function build(instance, baseElem, optionsOrMethod) {
2505
2521
  }
2506
2522
 
2507
2523
  /**
2508
- * A copy of the canvas, and the way from each of its elements back to
2509
- * the one it was copied from. The two trees are walked side by side,
2510
- * which works because a deep clone has the same shape.
2524
+ * A copy of the canvas without its drawers, and the way from each of
2525
+ * its elements back to the one it was copied from.
2526
+ *
2527
+ * Copied node by node rather than cloned whole: the drawers are most
2528
+ * of the canvas while it is edited, and a snapshot only took them off
2529
+ * again. A template is the one node a shallow clone leaves empty, so
2530
+ * it goes whole.
2511
2531
  */
2512
2532
  function copyCanvas() {
2513
- var copy = canvas.cloneNode(true);
2514
2533
  var toLive = new Map();
2515
2534
  var toCopy = new Map();
2516
- var live = document.createTreeWalker(canvas, NodeFilter.SHOW_ELEMENT);
2517
- var copied = document.createTreeWalker(copy, NodeFilter.SHOW_ELEMENT);
2518
- var a = live.currentNode;
2519
- var b = copied.currentNode;
2520
-
2521
- while (a && b) {
2522
- toLive.set(b, a);
2523
- toCopy.set(a, b);
2524
- a = live.nextNode();
2525
- b = copied.nextNode();
2535
+
2536
+ function copyNode(live) {
2537
+ var copied = live.cloneNode(live.nodeName === 'TEMPLATE');
2538
+ if (live.nodeType !== 1) { return copied; }
2539
+
2540
+ toLive.set(copied, live);
2541
+ toCopy.set(live, copied);
2542
+ if (live.nodeName === 'TEMPLATE') { return copied; }
2543
+
2544
+ for (var child = live.firstChild; child; child = child.nextSibling) {
2545
+ if (child.nodeType === 1 && dom.hasClass(child, 'ge-tools-drawer')) { continue; }
2546
+ copied.appendChild(copyNode(child));
2547
+ }
2548
+ return copied;
2526
2549
  }
2527
2550
 
2551
+ var copy = copyNode(canvas);
2552
+
2528
2553
  return {
2529
2554
  root: copy,
2530
2555
  liveOf: function(node) { return toLive.get(node) || null; },
@@ -4127,15 +4152,13 @@ function build(instance, baseElem, optionsOrMethod) {
4127
4152
 
4128
4153
  function flush() {
4129
4154
  frame = null;
4130
- var drawers = Array.from(due);
4131
- due.clear();
4132
- drawers.forEach(function(drawer) {
4133
- if (!drawer.isConnected) {
4134
- drawerResizes.unobserve(drawer);
4135
- return;
4136
- }
4137
- fitDrawer(drawer);
4155
+ var drawers = Array.from(due).filter(function(drawer) {
4156
+ if (drawer.isConnected) { return true; }
4157
+ drawerResizes.unobserve(drawer);
4158
+ return false;
4138
4159
  });
4160
+ due.clear();
4161
+ fitDrawers(drawers);
4139
4162
  }
4140
4163
 
4141
4164
  var drawerResizes = new window.ResizeObserver(function(entries) {
@@ -4178,40 +4201,73 @@ function build(instance, baseElem, optionsOrMethod) {
4178
4201
  });
4179
4202
  }
4180
4203
 
4181
- /** Hide the tools that don't fit on the drawer's line, from the end, behind its more tool. */
4182
- function fitDrawer(drawer) {
4183
- var tools = dom.children(drawer, 'a').filter(function(tool) { return !dom.hasClass(tool, 'ge-drawer-more'); });
4184
- var expanded = dom.hasClass(drawer, 'ge-drawer-expanded');
4185
-
4186
- // Measured on one line, with every tool back on it
4187
- dom.removeClass(drawer, 'ge-drawer-overflow ge-drawer-expanded');
4188
- tools.forEach(function(tool) { dom.removeClass(tool, 'ge-tool-overflow'); });
4189
-
4190
- if (!drawerOverflows(drawer, tools)) { return; }
4204
+ /**
4205
+ * Hide the tools that don't fit on each drawer's line, from the end,
4206
+ * behind its more tool.
4207
+ *
4208
+ * All the drawers at once, in phases - every write, then every read -
4209
+ * so the page is laid out three times however many drawers there
4210
+ * are: a drawer at a time, each measure laid out the whole page
4211
+ * again, and a canvas of a few hundred drawers took seconds. One
4212
+ * measure of a drawer is enough: its tools are on one line, aligned
4213
+ * left, so hiding the last ones or making room for the more tool at
4214
+ * the end moves none of the others.
4215
+ */
4216
+ function fitDrawers(drawers) {
4217
+ // Every tool back on the one line
4218
+ var fits = drawers.map(function(drawer) {
4219
+ var fit = {
4220
+ drawer: drawer,
4221
+ tools: dom.children(drawer, 'a').filter(function(tool) { return !dom.hasClass(tool, 'ge-drawer-more'); }),
4222
+ expanded: dom.hasClass(drawer, 'ge-drawer-expanded'),
4223
+ };
4224
+ dom.removeClass(drawer, 'ge-drawer-overflow ge-drawer-expanded');
4225
+ fit.tools.forEach(function(tool) { dom.removeClass(tool, 'ge-tool-overflow'); });
4226
+ return fit;
4227
+ });
4191
4228
 
4192
- dom.addClass(drawer, 'ge-drawer-overflow');
4193
- if (!dom.child(drawer, '.ge-drawer-more')) {
4194
- createTool(drawer, t('tool.more'), 'ge-drawer-more', 'bi bi-three-dots', function() {
4195
- dom.toggleClass(drawer, 'ge-drawer-expanded');
4229
+ // Where each tool ends, and whether any ends past the drawer's room
4230
+ var over = fits.filter(function(fit) {
4231
+ fit.rights = fit.tools.map(function(tool) {
4232
+ var rects = tool.getClientRects();
4233
+ return rects.length ? rects[0].right : null;
4196
4234
  });
4197
- }
4235
+ return overflows(fit, roomOf(fit.drawer));
4236
+ });
4198
4237
 
4199
- for (var i = tools.length - 1; i >= 0 && drawerOverflows(drawer, tools); i--) {
4200
- dom.addClass(tools[i], 'ge-tool-overflow');
4201
- }
4238
+ // The more tool takes its room at the end, and the tools that
4239
+ // still end past it are hidden, from the last
4240
+ over.forEach(function(fit) { dom.addClass(fit.drawer, 'ge-drawer-overflow'); });
4241
+ over.forEach(function(fit) { fit.room = roomOf(fit.drawer); });
4242
+ over.forEach(function(fit) {
4243
+ for (var i = fit.tools.length - 1; i >= 0 && overflows(fit, fit.room, i); i--) {
4244
+ dom.addClass(fit.tools[i], 'ge-tool-overflow');
4245
+ }
4246
+ if (!dom.child(fit.drawer, '.ge-drawer-more')) {
4247
+ createTool(fit.drawer, t('tool.more'), 'ge-drawer-more', 'bi bi-three-dots', function() {
4248
+ dom.toggleClass(fit.drawer, 'ge-drawer-expanded');
4249
+ });
4250
+ }
4202
4251
 
4203
- dom.toggleClass(drawer, 'ge-drawer-expanded', expanded);
4252
+ // Unfolded it stays, while there is something to unfold
4253
+ dom.toggleClass(fit.drawer, 'ge-drawer-expanded', fit.expanded);
4254
+ });
4204
4255
  }
4205
4256
 
4206
- function drawerOverflows(drawer, tools) {
4257
+ /** Where a drawer's tools have to end: its right edge, inside its border and padding. */
4258
+ function roomOf(drawer) {
4207
4259
  var style = window.getComputedStyle(drawer);
4208
- var limit = drawer.getBoundingClientRect().right -
4260
+ return drawer.getBoundingClientRect().right -
4209
4261
  parseFloat(style.borderRightWidth) - parseFloat(style.paddingRight);
4262
+ }
4210
4263
 
4211
- return tools.some(function(tool) {
4212
- var rects = tool.getClientRects();
4213
- return rects.length > 0 && rects[0].right > limit + 0.5;
4214
- });
4264
+ /** Whether any of a drawer's tools up to `last` ends past `room`. */
4265
+ function overflows(fit, room, last) {
4266
+ last = last === undefined ? fit.rights.length - 1 : last;
4267
+ for (var i = 0; i <= last; i++) {
4268
+ if (fit.rights[i] !== null && fit.rights[i] > room + 0.5) { return true; }
4269
+ }
4270
+ return false;
4215
4271
  }
4216
4272
 
4217
4273
  /**
@@ -55,13 +55,20 @@ export function drawerflowPart(ge) {
55
55
  return !(dom.hasClass(node, 'row') && style.flexDirection === 'row' && style.flexWrap === 'wrap');
56
56
  }
57
57
 
58
- function measure(node, drawer) {
59
- var room = Math.ceil(drawer.offsetHeight) + GAP;
58
+ /** The room a drawer takes above its node's content. */
59
+ function roomOf(drawer) {
60
+ return Math.ceil(drawer.offsetHeight) + GAP;
61
+ }
60
62
 
63
+ function place(node, room) {
61
64
  rule(room);
62
65
  node.setAttribute(OUT_ATTR, String(room));
63
66
  }
64
67
 
68
+ function measure(node, drawer) {
69
+ place(node, roomOf(drawer));
70
+ }
71
+
65
72
  /** A drawer that grows - its tools wrap, its settings unfold - takes more room. */
66
73
  function watch(drawer) {
67
74
  if (typeof ResizeObserver === 'undefined') { return; }
@@ -77,22 +84,36 @@ export function drawerflowPart(ge) {
77
84
  observer.observe(drawer);
78
85
  }
79
86
 
87
+ /**
88
+ * Every read, then every write: a node at a time, each read laid out the
89
+ * whole page again after the last node's write, which on a large canvas
90
+ * took longer than everything else a refresh does.
91
+ */
80
92
  function mark(scope) {
81
- dom.selfAndAll(scope, NODES).forEach(function(node) {
93
+ var nodes = dom.selfAndAll(scope, NODES).map(function(node) {
82
94
  var drawer = dom.child(node, '.ge-tools-drawer');
83
- if (!drawer) { return; }
84
-
85
- if (laysOut(node)) {
86
- dom.addClass(node, OUT_CLASS);
87
- measure(node, drawer);
88
- watch(drawer);
89
- } else if (dom.hasClass(node, OUT_CLASS)) {
90
- dom.removeClass(node, OUT_CLASS);
91
- node.removeAttribute(OUT_ATTR);
92
- dom.dropEmptyClass(node);
93
- if (observer) { observer.unobserve(drawer); }
95
+ return drawer ? { node: node, drawer: drawer, out: laysOut(node) } : null;
96
+ }).filter(Boolean);
97
+
98
+ var out = nodes.filter(function(entry) { return entry.out; });
99
+
100
+ nodes.forEach(function(entry) {
101
+ if (entry.out) {
102
+ dom.addClass(entry.node, OUT_CLASS);
103
+ } else if (dom.hasClass(entry.node, OUT_CLASS)) {
104
+ dom.removeClass(entry.node, OUT_CLASS);
105
+ entry.node.removeAttribute(OUT_ATTR);
106
+ dom.dropEmptyClass(entry.node);
107
+ if (observer) { observer.unobserve(entry.drawer); }
94
108
  }
95
109
  });
110
+
111
+ // Out of the flow its drawer is as wide as the node, and may wrap
112
+ out.forEach(function(entry) { entry.room = roomOf(entry.drawer); });
113
+ out.forEach(function(entry) {
114
+ place(entry.node, entry.room);
115
+ watch(entry.drawer);
116
+ });
96
117
  }
97
118
 
98
119
  function unmarkNodes(root) {
@@ -24,7 +24,8 @@
24
24
  * `source: 'paste'` - so a host can turn one away.
25
25
  *
26
26
  * With active_target (which the sidebar turns on), Ctrl+C copies the target,
27
- * Ctrl+X cuts it and Ctrl+V pastes into it, as the toolbar adds to it.
27
+ * Ctrl+X cuts it and Ctrl+V pastes into it, as the toolbar adds to it, or
28
+ * with no target at the end of the canvas.
28
29
  * `clipboard: { shortcuts: false }` turns them off. They go through the same
29
30
  * store as the tools, never the system clipboard.
30
31
  */
@@ -233,13 +234,14 @@ GridEditor.features.clipboard = function(ge) {
233
234
  * Ctrl+V: into the target, where the toolbar would add it - at the end
234
235
  * of what the target holds, in the row or column it lacks, a section on
235
236
  * the canvas after the block the target is in. What the target cannot
236
- * take is not pasted anywhere else.
237
+ * take is not pasted anywhere else. With no target, at the end of the
238
+ * canvas, as the toolbar's paste buttons do.
237
239
  */
238
240
  function pasteByShortcut() {
239
241
  var clip = read();
240
- if (!clip || !ge.getActiveTarget() || !available(clip)) { return false; }
242
+ if (!clip || !available(clip)) { return false; }
241
243
 
242
- var made = ge.placeInTarget(ge.freshNode(clip.html), clip.kind, 'paste');
244
+ var made = ge.placeInTarget(ge.freshNode(clip.html), clip.kind, 'paste', { fallback: 'canvas' });
243
245
  if (made && made.scrollIntoView) { made.scrollIntoView({ behavior: 'smooth' }); }
244
246
 
245
247
  return true;