@themarioga/grid-editor 9.0.0-beta.4 → 9.0.0-beta.5

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.
@@ -467,6 +467,7 @@ function build(instance, baseElem, optionsOrMethod) {
467
467
  var confirmDialog = null; // The delete confirmation, built when first needed
468
468
  var sizePicker = null; // The open column size picker, if there is one
469
469
  var sourceOpen = false; // Whether the canvas is being edited as html
470
+ var previewing = false; // Whether the preview button is pressed: not while the mouse is only over it
470
471
  var dropMarker = null; // The line showing where a dragged toolbar button would land
471
472
  var activeTarget = null; // The column or region the toolbar adds to, with active_target
472
473
  var toolbarLifetime = null; // Aborted when the toolbar is rebuilt or taken away
@@ -1338,21 +1339,20 @@ function build(instance, baseElem, optionsOrMethod) {
1338
1339
  btnGroup.appendChild(htmlButton);
1339
1340
  }
1340
1341
 
1342
+ // Over it, the canvas looks as the page will; pressed, it stays
1343
+ // that way and the page's own controls work
1341
1344
  var previewButton = dom.create('<button type="button" class="btn btn-sm btn-primary gm-preview"><i class="bi bi-eye-fill"></i></button>');
1342
- var endPreview = function() {
1343
- if (!dom.hasClass(previewButton, 'active')) {
1344
- dom.addClass(canvas, 'ge-editing');
1345
- }
1346
- };
1347
1345
  previewButton.setAttribute('title', t('tool.preview'));
1346
+ dom.toggleClass(previewButton, 'active btn-danger', previewing);
1348
1347
  previewButton.addEventListener('mouseenter', function() {
1349
1348
  dom.removeClass(canvas, 'ge-editing');
1350
1349
  });
1351
1350
  previewButton.addEventListener('click', function() {
1352
- dom.toggleClass(previewButton, 'active btn-danger');
1353
- endPreview();
1351
+ setPreview(!previewing);
1352
+ });
1353
+ previewButton.addEventListener('mouseleave', function() {
1354
+ if (!previewing) { dom.addClass(canvas, 'ge-editing'); }
1354
1355
  });
1355
- previewButton.addEventListener('mouseleave', endPreview);
1356
1356
  btnGroup.appendChild(previewButton);
1357
1357
 
1358
1358
  // Floated right after the source and preview buttons, so it
@@ -1467,14 +1467,8 @@ function build(instance, baseElem, optionsOrMethod) {
1467
1467
  // then - or taken away for good
1468
1468
  if (!mainControls || destroyed) { return; }
1469
1469
 
1470
- var previewing = !!dom.one(mainControls, '.gm-preview.active');
1471
-
1472
1470
  removeMainControls();
1473
1471
  createMainControls();
1474
-
1475
- if (previewing) {
1476
- dom.addClass(dom.one(mainControls, '.gm-preview'), 'active btn-danger');
1477
- }
1478
1472
  onScroll();
1479
1473
  }
1480
1474
 
@@ -2163,6 +2157,25 @@ function build(instance, baseElem, optionsOrMethod) {
2163
2157
  init();
2164
2158
  }
2165
2159
 
2160
+ /**
2161
+ * The preview button pressed, or let go: the one way the preview
2162
+ * changes, told only when it does. Pressed, the canvas stops looking
2163
+ * edited and a plugin lets the page's own controls work - a popup's
2164
+ * trigger opens it - until it is let go. Only hovering the button
2165
+ * previews the look alone, and tells no one.
2166
+ */
2167
+ function setPreview(on) {
2168
+ on = !!on;
2169
+ if (on === previewing) { return; }
2170
+
2171
+ previewing = on;
2172
+ var button = mainControls ? dom.one(mainControls, '.gm-preview') : null;
2173
+ if (button) { dom.toggleClass(button, 'active btn-danger', on); }
2174
+ dom.toggleClass(canvas, 'ge-editing', !on);
2175
+ plugins('onPreviewChange', on);
2176
+ emit('preview-change', { canvas: canvas, previewing: on });
2177
+ }
2178
+
2166
2179
  /**
2167
2180
  * The canvas as html, in the textarea in its place, to edit by hand.
2168
2181
  * A feature plugin's onSourceOpen may put a code editor over the
@@ -2170,6 +2183,7 @@ function build(instance, baseElem, optionsOrMethod) {
2170
2183
  * what the canvas is made of again.
2171
2184
  */
2172
2185
  function openSource() {
2186
+ setPreview(false);
2173
2187
  deinit();
2174
2188
  // The canvas is made again from the html when it comes back
2175
2189
  changeTarget(null);
@@ -2245,7 +2259,8 @@ function build(instance, baseElem, optionsOrMethod) {
2245
2259
  }
2246
2260
 
2247
2261
  runFilter(true);
2248
- dom.addClass(canvas, 'ge-editing');
2262
+ // A getHtml in the middle of a preview comes back to it
2263
+ dom.toggleClass(canvas, 'ge-editing', !previewing);
2249
2264
  dom.toggleClass(canvas, 'ge-drag-drawer', settings.drag_handle === 'drawer');
2250
2265
  addAllColClasses();
2251
2266
  var cutter = textCutter();
@@ -2465,6 +2480,8 @@ function build(instance, baseElem, optionsOrMethod) {
2465
2480
  }
2466
2481
  }
2467
2482
 
2483
+ // What a preview opened goes with the markup it was in
2484
+ setPreview(false);
2468
2485
  deinit();
2469
2486
  dom.setHtml(canvas, html);
2470
2487
  init();
@@ -2481,6 +2498,7 @@ function build(instance, baseElem, optionsOrMethod) {
2481
2498
  plugins('onDestroy');
2482
2499
  // The html being edited is what the canvas is left with
2483
2500
  if (sourceOpen) { closeSource(); }
2501
+ setPreview(false);
2484
2502
  deinit();
2485
2503
  activeTarget = null;
2486
2504
  selected = null;
@@ -2593,6 +2611,7 @@ function build(instance, baseElem, optionsOrMethod) {
2593
2611
  operate: operate,
2594
2612
  kindOf: kindOf,
2595
2613
  view: getView,
2614
+ previewing: function() { return previewing; },
2596
2615
  viewTiers: function() {
2597
2616
  return tiersFor(curView).map(function(tier) { return tier.key; });
2598
2617
  },
@@ -328,6 +328,9 @@ GridEditor.containers.offcanvas = function(ge) {
328
328
  dropTriggerMarks(ge.canvas);
329
329
  },
330
330
 
331
+ // The preview pressed, the triggers open the real thing; let go, it closes
332
+ onPreviewChange: triggers.onPreviewChange,
333
+
331
334
  // What onDeinit and unmark do, on a copy of the canvas
332
335
  cleanMarkup: function(root) {
333
336
  var definition = this;
@@ -32,6 +32,8 @@ GridEditor.containers.popup = function(ge) {
32
32
 
33
33
  onInit: triggers.onInit,
34
34
  onDeinit: triggers.onDeinit,
35
+ // The preview pressed, the triggers open the real thing; let go, it closes
36
+ onPreviewChange: triggers.onPreviewChange,
35
37
 
36
38
  // What onDeinit and unmark do, on a copy of the canvas
37
39
  cleanMarkup: function(root) {
@@ -3,7 +3,9 @@
3
3
  *
4
4
  * A trigger is markup the host owns - a button, a link - anywhere in the
5
5
  * canvas, that points at a container by id. While editing it must not reach
6
- * Bootstrap, and one that points at nothing is warned about. Not a public
6
+ * Bootstrap, and one that points at nothing is warned about. With the
7
+ * preview button pressed it does reach it, and opens the container as the
8
+ * page will, until the button is let go. Not a public
7
9
  * API: the build puts a copy of this file in each plugin that imports it,
8
10
  * so it keeps nothing of its own between plugins.
9
11
  */
@@ -18,19 +20,47 @@ import * as dom from '../dom.js';
18
20
  * `targetAttribute` the one a trigger names it with, and `toggle` is
19
21
  * Bootstrap's `data-bs-toggle` for it, written on the way out.
20
22
  *
21
- * Gives back `onInit` and `onDeinit`, for the plugin to hand the editor as
22
- * its own, and `write(root)`, what onDeinit does to the canvas, for
23
- * cleanMarkup to do to a copy.
23
+ * Gives back `onInit`, `onDeinit` and `onPreviewChange`, for the plugin to
24
+ * hand the editor as its own, and `write(root)`, what onDeinit does to the
25
+ * canvas, for cleanMarkup to do to a copy.
24
26
  */
25
27
  export function triggersFor(ge, kind, idAttribute, targetAttribute, toggle) {
26
28
  var triggerClass = 'ge-' + kind + '-trigger';
27
29
  var orphanClass = 'ge-' + kind + '-orphan';
28
30
  var listening = null;
29
31
 
32
+ // What Bootstrap changes on a container as it opens and closes it
33
+ var STATE_ATTRIBUTES = ['style', 'aria-hidden', 'aria-modal', 'role'];
34
+ var STATE_CLASSES = ['show', 'showing', 'hiding'];
35
+ // Each container's element as the preview found it: what it goes back to
36
+ var before = new Map();
37
+ // The dismisses the preview gave back to Bootstrap, to take away again
38
+ var dismisses = [];
39
+ // What a getHtml in the middle of a preview took off, for onInit to put back
40
+ var saved = new Map();
41
+ // Where Bootstrap is with each container it has been asked to open:
42
+ // opening, open or closing, as its events tell. Not there once closed
43
+ var phases = new Map();
44
+ var watching = null;
45
+ // Let go of while Bootstrap was still closing them: what they go back to once it is done
46
+ var pending = new Map();
47
+ var PHASES = { show: 'opening', shown: 'open', hide: 'closing' };
48
+
30
49
  function idOf(container) {
31
50
  return container.getAttribute(idAttribute);
32
51
  }
33
52
 
53
+ /** The modal, the panel: the element in a container with the container's id. */
54
+ function targetOf(container) {
55
+ var id = idOf(container);
56
+
57
+ return dom.all(container, '[id]').filter(function(node) { return node.id === id; })[0] || null;
58
+ }
59
+
60
+ function targets() {
61
+ return dom.all(ge.canvas, '[' + idAttribute + ']').map(targetOf).filter(Boolean);
62
+ }
63
+
34
64
  /** Whether a container with this id is on the canvas. */
35
65
  function exists(id, root) {
36
66
  return dom.all(root || ge.canvas, '[' + idAttribute + ']').some(function(container) {
@@ -74,24 +104,170 @@ export function triggersFor(ge, kind, idAttribute, targetAttribute, toggle) {
74
104
  });
75
105
  }
76
106
 
107
+ /** Bootstrap's own attributes on the triggers that point at something. */
108
+ function arm(root) {
109
+ dom.all(root, '[' + targetAttribute + ']').forEach(function(trigger) {
110
+ var wanted = trigger.getAttribute(targetAttribute);
111
+ if (!exists(wanted, root)) { return; }
112
+
113
+ trigger.setAttribute('data-bs-toggle', toggle);
114
+ trigger.setAttribute('data-bs-target', '#' + wanted);
115
+ });
116
+ }
117
+
118
+ function disarm() {
119
+ dom.all(ge.canvas, '[' + targetAttribute + ']').forEach(function(trigger) {
120
+ trigger.removeAttribute('data-bs-toggle');
121
+ trigger.removeAttribute('data-bs-target');
122
+ });
123
+ }
124
+
77
125
  function write(root) {
78
126
  root = root || ge.canvas;
79
127
 
128
+ // The warning marking is editing furniture, whether or not
129
+ // the trigger can be wired up
80
130
  dom.all(root, '[' + targetAttribute + ']').forEach(function(trigger) {
81
- var wanted = trigger.getAttribute(targetAttribute);
82
-
83
- // The warning marking is editing furniture, whether or not
84
- // the trigger can be wired up
85
131
  dom.removeClass(trigger, orphanClass);
86
132
  dom.dropEmptyClass(trigger);
133
+ });
87
134
 
88
- if (!exists(wanted, root)) { return; }
135
+ arm(root);
89
136
 
90
- trigger.setAttribute('data-bs-toggle', toggle);
91
- trigger.setAttribute('data-bs-target', '#' + wanted);
137
+ // A container the preview opened is read as the preview found it,
138
+ // whether the preview goes on or Bootstrap is still closing it
139
+ var quiet = new Map();
140
+ before.forEach(function(state, target) { quiet.set(target.id, state); });
141
+ pending.forEach(function(state, target) { quiet.set(target.id, state); });
142
+ if (!quiet.size) { return; }
143
+
144
+ dom.all(root, '[id]').forEach(function(node) {
145
+ if (quiet.has(node.id)) { putState(node, quiet.get(node.id)); }
92
146
  });
93
147
  }
94
148
 
149
+ /** What Bootstrap may change on a container, as it is now. */
150
+ function stateOf(target) {
151
+ var state = { classes: STATE_CLASSES.filter(function(name) { return dom.hasClass(target, name); }) };
152
+ STATE_ATTRIBUTES.forEach(function(name) { state[name] = target.getAttribute(name); });
153
+ return state;
154
+ }
155
+
156
+ function putState(target, state) {
157
+ STATE_CLASSES.forEach(function(name) { dom.toggleClass(target, name, state.classes.indexOf(name) !== -1); });
158
+ STATE_ATTRIBUTES.forEach(function(name) {
159
+ // Read first: a style Bootstrap last wrote through the element's
160
+ // style object is only written to the attribute when it is read,
161
+ // and would otherwise come back, empty, after it is taken off
162
+ target.getAttribute(name);
163
+
164
+ if (state[name] === null) {
165
+ target.removeAttribute(name);
166
+ } else {
167
+ target.setAttribute(name, state[name]);
168
+ }
169
+ });
170
+ }
171
+
172
+ /**
173
+ * The preview pressed: the triggers reach Bootstrap, and so do the
174
+ * container's own close buttons, which editing keeps from it.
175
+ */
176
+ function startPreview() {
177
+ // Again after a getHtml, whose init took them away once more
178
+ dismisses = [];
179
+ targets().forEach(function(target) {
180
+ if (!before.has(target)) { before.set(target, stateOf(target)); }
181
+
182
+ dom.all(target, '[data-ge-bs-dismiss="' + toggle + '"]').forEach(function(dismiss) {
183
+ ge.resumeToggles(dismiss);
184
+ dismisses.push(dismiss);
185
+ });
186
+ });
187
+ arm(ge.canvas);
188
+
189
+ if (watching) { watching.abort(); }
190
+ watching = new AbortController();
191
+ ['show', 'shown', 'hide', 'hidden'].forEach(function(name) {
192
+ ge.canvas.addEventListener(name + '.bs.' + toggle, function(e) {
193
+ if (!before.has(e.target)) { return; }
194
+
195
+ if (name === 'hidden') {
196
+ phases.delete(e.target);
197
+ } else {
198
+ phases.set(e.target, PHASES[name]);
199
+ }
200
+ }, { signal: watching.signal });
201
+ });
202
+ }
203
+
204
+ /** Bootstrap's component for this kind, when the page has it on window. */
205
+ function component() {
206
+ var bootstrap = window.bootstrap;
207
+ var name = toggle.charAt(0).toUpperCase() + toggle.slice(1);
208
+
209
+ return bootstrap && bootstrap[name] && bootstrap[name].getInstance ? bootstrap[name] : null;
210
+ }
211
+
212
+ /**
213
+ * Ask Bootstrap to close a container: through its instance, or, for a
214
+ * page with no window.bootstrap - an app that imports it - through its
215
+ * own close button, which its data api still listens for.
216
+ */
217
+ function closer(target) {
218
+ var Component = component();
219
+ var instance = Component ? Component.getInstance(target) : null;
220
+ if (instance) { return function() { instance.hide(); }; }
221
+
222
+ var dismiss = dom.one(target, '[data-bs-dismiss="' + toggle + '"], [data-ge-bs-dismiss="' + toggle + '"]');
223
+ if (!dismiss) { return null; }
224
+
225
+ return function() {
226
+ ge.resumeToggles(dismiss);
227
+ dismiss.click();
228
+ ge.suspendToggles(dismiss);
229
+ };
230
+ }
231
+
232
+ /**
233
+ * The preview let go: an open container is closed, by Bootstrap so its
234
+ * backdrop and the page's scroll go back too, and each is left as the
235
+ * preview found it once Bootstrap is done with it. One still opening is
236
+ * closed when it has opened, as Bootstrap will not close it before.
237
+ */
238
+ function endPreview() {
239
+ if (watching) { watching.abort(); }
240
+ watching = null;
241
+
242
+ before.forEach(function(state, target) {
243
+ var phase = phases.get(target);
244
+ var close = phase && phase !== 'closing' ? closer(target) : null;
245
+ var restore = function() {
246
+ pending.delete(target);
247
+ putState(target, state);
248
+ };
249
+
250
+ if (!phase || (phase !== 'closing' && !close)) {
251
+ restore();
252
+ return;
253
+ }
254
+
255
+ pending.set(target, state);
256
+ target.addEventListener('hidden.bs.' + toggle, restore, { once: true });
257
+ if (phase === 'opening') {
258
+ target.addEventListener('shown.bs.' + toggle, close, { once: true });
259
+ } else if (phase === 'open') {
260
+ close();
261
+ }
262
+ });
263
+ before = new Map();
264
+ phases = new Map();
265
+
266
+ dismisses.forEach(function(dismiss) { ge.suspendToggles(dismiss); });
267
+ dismisses = [];
268
+ disarm();
269
+ }
270
+
95
271
  return {
96
272
  write: write,
97
273
 
@@ -109,13 +285,37 @@ export function triggersFor(ge, kind, idAttribute, targetAttribute, toggle) {
109
285
  dom.delegate(ge.canvas, 'click', '.' + triggerClass + ', [' + targetAttribute + ']', function(e) {
110
286
  if (dom.hasClass(ge.canvas, 'ge-editing')) { e.preventDefault(); }
111
287
  }, { signal: listening.signal });
288
+
289
+ // A getHtml in the middle of a preview: back to what it was
290
+ if (ge.previewing()) {
291
+ var live = new Map(saved);
292
+ saved = new Map();
293
+ startPreview();
294
+ live.forEach(function(state, target) {
295
+ if (ge.canvas.contains(target)) { putState(target, state); }
296
+ });
297
+ }
112
298
  },
113
299
 
114
300
  onDeinit: function() {
115
301
  if (listening) { listening.abort(); }
116
302
  listening = null;
117
303
 
304
+ // In the middle of a preview, write reads the markup as the
305
+ // preview found it, not with a container open: onInit puts it back
306
+ if (ge.previewing()) {
307
+ before.forEach(function(state, target) { saved.set(target, stateOf(target)); });
308
+ }
309
+
118
310
  write();
119
311
  },
312
+
313
+ onPreviewChange: function(previewing) {
314
+ if (previewing) {
315
+ startPreview();
316
+ } else {
317
+ endPreview();
318
+ }
319
+ },
120
320
  };
121
321
  }
@@ -396,6 +396,12 @@ export interface TargetChangePayload {
396
396
  from: HTMLElement | null;
397
397
  }
398
398
 
399
+ /** The preview button was pressed (true) or let go (false). */
400
+ export interface PreviewChangePayload {
401
+ canvas: HTMLElement;
402
+ previewing: boolean;
403
+ }
404
+
399
405
  /** The selection changed, with settings_panel 'sidebar': the node whose settings the sidebar shows, or null. */
400
406
  export interface SelectionChangePayload {
401
407
  canvas: HTMLElement;
@@ -443,6 +449,7 @@ export interface GridEditorEventMap {
443
449
  'grideditor:popup-orphan': PopupOrphanPayload;
444
450
  'grideditor:offcanvas-orphan': OffcanvasOrphanPayload;
445
451
  'grideditor:view-change': ViewChangePayload;
452
+ 'grideditor:preview-change': PreviewChangePayload;
446
453
  'grideditor:target-change': TargetChangePayload;
447
454
  'grideditor:selection-change': SelectionChangePayload;
448
455
  'grideditor:after-autosave': AutosavePayload;