@yuneta/gobj-ui 7.26.56 → 7.26.58

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.
@@ -646,9 +646,14 @@ function import_locales(locales_dir, codes)
646
646
  /***************************************************************
647
647
  * The code of the locales' process (import_locales()), run there
648
648
  * from its text: it can use nothing of this module but its
649
- * argument and the nonce on its stdin. What it calls after the
650
- * locales ran is taken BEFORE them, so a locale that replaces
649
+ * argument and the nonce on its stdin. What it calls by name after
650
+ * the locales ran is taken BEFORE them, so a locale that replaces
651
651
  * process.reallyExit or JSON.stringify does not reach the report,
652
+ * and the text is encoded by a TextEncoder taken then too, not by
653
+ * Buffer (which looks Buffer.prototype.utf8Write up when it runs).
654
+ * That covers what this code names, not every lookup Node makes
655
+ * inside it: a locale that sets out to forge the report can (the
656
+ * README says what is out of reach),
652
657
  * and the report is written by hand from strings (JSON.stringify of
653
658
  * a primitive string reads no toJSON): a toJSON a locale puts on
654
659
  * Object.prototype or Array.prototype is never called. It reports after a
@@ -666,17 +671,19 @@ async function report_locales(request)
666
671
  const keys_of = Object.keys;
667
672
  const set_timeout = setTimeout;
668
673
  const set_immediate = setImmediate;
669
- const buffer_from = Buffer.from.bind(Buffer);
674
+ const encoder = new TextEncoder();
675
+ const encode = encoder.encode.bind(encoder);
670
676
  const define = Object.defineProperty;
671
677
  const has_own = Object.hasOwn;
678
+ const to_string = String;
672
679
  const problems = [];
673
680
  const add_problem = (text) => {
674
- define(problems, problems.length, {value: String(text), enumerable: true});
681
+ define(problems, problems.length, {value: to_string(text), enumerable: true});
675
682
  };
676
683
  const list_text = (list) => {
677
684
  let text = "[";
678
685
  for(let i = 0; i < list.length; i++) {
679
- text += (i? "," : "") + stringify(String(list[i]));
686
+ text += (i? "," : "") + stringify(to_string(list[i]));
680
687
  }
681
688
  return text + "]";
682
689
  };
@@ -712,7 +719,7 @@ async function report_locales(request)
712
719
  });
713
720
  }
714
721
  } catch(e) {
715
- set_locale(code, {error: String((e && e.message) || e)});
722
+ set_locale(code, {error: to_string((e && e.message) || e)});
716
723
  }
717
724
  }
718
725
  set_timeout(() => {
@@ -727,7 +734,7 @@ async function report_locales(request)
727
734
  `{"keys":${list_text(one.keys)},"name":${one.name === null? "null" : stringify(one.name)}}`
728
735
  );
729
736
  }
730
- const buf = buffer_from(text + "}}\n", "utf8");
737
+ const buf = encode(text + "}}\n");
731
738
  let done = 0;
732
739
  while(done < buf.length) {
733
740
  done += writeSync(3, buf, done, buf.length - done);
package/src/c_yui_form.js CHANGED
@@ -100,7 +100,7 @@ import {
100
100
  time_value_to_text
101
101
  } from "./form_time_value.js";
102
102
  import {form_time_cell_editor} from "./form_time_cell_editor.js";
103
- import {yui_tabulator_keep_edit_on_redraw} from "./yui_tabulator_edit.js";
103
+ import {yui_tabulator_keep_edit_on_redraw, yui_tabulator_close_editor} from "./yui_tabulator_edit.js";
104
104
  import {safe_locale} from "./yui_date_text.js";
105
105
  import {parse_json_field, json_editor_value} from "./form_json_field.js";
106
106
  import {
@@ -652,33 +652,6 @@ function destroy_widget(gobj, what, widget)
652
652
  }
653
653
  }
654
654
 
655
- /************************************************************
656
- * Close the cell editor a table has open, dropping what is
657
- * typed in it, before its rows are wiped. Tabulator 6.6.0
658
- * closes it while it wipes them, and the redraw that follows
659
- * walks the rows already wiped: a reload, a Clear or a destroy
660
- * with an editor open in any row but the first threw, and left
661
- * the table half wiped.
662
- *
663
- * Tabulator has no public way to ask which cell is edited: this
664
- * reads its edit module (`modules.edit`, `currentCell`,
665
- * `cancelEdit()`), names pinned against the installed Tabulator
666
- * by tabulator_edit_internals.test.js. A Tabulator without them
667
- * is said: the guard would do nothing, and the crash come back.
668
- ************************************************************/
669
- function close_table_editor(gobj, tabulator)
670
- {
671
- let edit = tabulator.modules? tabulator.modules.edit : null;
672
- if(!edit || !("currentCell" in edit) || typeof edit.cancelEdit !== "function") {
673
- log_warning(`${gobj_short_name(gobj)}: this Tabulator has no modules.edit.currentCell/cancelEdit(): ` +
674
- `an open cell editor is not closed before its table is wiped`);
675
- return;
676
- }
677
- if(edit.currentCell) {
678
- edit.cancelEdit();
679
- }
680
- }
681
-
682
655
  /************************************************************
683
656
  * The element of the table (`.tabulator` kept on it) that holds
684
657
  * $el, the innermost one.
@@ -719,7 +692,7 @@ function destroy_row_tables(gobj, $row)
719
692
  $row.yui_nested = null;
720
693
  for(let $nested of nested) {
721
694
  if($nested.tabulator) {
722
- close_table_editor(gobj, $nested.tabulator);
695
+ yui_tabulator_close_editor($nested.tabulator, gobj_short_name(gobj));
723
696
  destroy_rows_tables(gobj, $nested.tabulator);
724
697
  destroy_widget(gobj, "nested table", $nested.tabulator);
725
698
  $nested.tabulator = null;
@@ -758,7 +731,7 @@ function destroy_widgets(gobj, $container)
758
731
  $el.jsoneditor = null;
759
732
  }
760
733
  if($el.tabulator) {
761
- close_table_editor(gobj, $el.tabulator);
734
+ yui_tabulator_close_editor($el.tabulator, gobj_short_name(gobj));
762
735
  destroy_rows_tables(gobj, $el.tabulator);
763
736
  destroy_widget(gobj, "table", $el.tabulator);
764
737
  $el.tabulator = null;
@@ -2532,7 +2505,7 @@ function create_tabulator(gobj, $extend, name, template, rows)
2532
2505
  });
2533
2506
  tabulator.on("cellEditCancelled", function() {
2534
2507
  if(gobj_is_destroying(gobj)) {
2535
- return; // closed by the teardown (close_table_editor)
2508
+ return; // closed by the teardown (yui_tabulator_close_editor)
2536
2509
  }
2537
2510
  gobj_send_event(gobj, "EV_CELL_EDIT_CANCELLED",
2538
2511
  {table_id: table_identity(gobj, $extend)}, gobj);
@@ -2613,7 +2586,7 @@ const TIME_CELL_FORMATTERS = new WeakSet();
2613
2586
 
2614
2587
  /******************************************************************
2615
2588
  * The formatter of a time cell: the local clock, 24 hours, in the
2616
- * APP's language (fmt_date_time of yui_time.js, as the cells of
2589
+ * APP's language (fmt_date_time of yui_date_text.js, as the cells of
2617
2590
  * C_YUI_TREEDB_TOPIC_WITH_FORM): es "14 oct 2026, 15:05:09".
2618
2591
  ******************************************************************/
2619
2592
  function time_cell_formatter(real_type)
@@ -3403,7 +3376,7 @@ function clear_data(gobj, $form)
3403
3376
  $form.querySelectorAll('.yui-tabulator-table').forEach($table => {
3404
3377
  $table.yui_pending_rows = null;
3405
3378
  if($table.tabulator.initialized) {
3406
- close_table_editor(gobj, $table.tabulator);
3379
+ yui_tabulator_close_editor($table.tabulator, gobj_short_name(gobj));
3407
3380
  destroy_rows_tables(gobj, $table.tabulator);
3408
3381
  $table.tabulator.clearData();
3409
3382
  }
@@ -3531,6 +3504,11 @@ function set_form_values(gobj, template, $form, record)
3531
3504
  $input.value = date_to_datetime_local(value);
3532
3505
  $input.yui_time_kept = ($input.value !== "")?
3533
3506
  {value: stored, shown: $input.value} : null;
3507
+ if($input.value === "" && stored !== null && stored !== "") {
3508
+ log_warning(`${gobj_short_name(gobj)}: '${path}' holds ` +
3509
+ `${JSON.stringify(stored)}, which the time box cannot show: ` +
3510
+ `a save writes it as null`);
3511
+ }
3534
3512
  break;
3535
3513
  case "color":
3536
3514
  $input.value = convertColor(value);
@@ -79,7 +79,7 @@ import {yui_tabulator_lang, yui_tabulator_relocalize,
79
79
  yui_tabulator_name_filters,
80
80
  yui_tabulator_name_row_selects,
81
81
  yui_tabulator_name_editor} from "./yui_tabulator_i18n.js";
82
- import {yui_tabulator_keep_edit_on_redraw} from "./yui_tabulator_edit.js";
82
+ import {yui_tabulator_keep_edit_on_redraw, yui_tabulator_close_editor} from "./yui_tabulator_edit.js";
83
83
 
84
84
  import {
85
85
  yui_selection_column,
@@ -2021,7 +2021,12 @@ function table__destroy(gobj)
2021
2021
  {
2022
2022
  let tabulator = gobj_read_attr(gobj, "tabulator");
2023
2023
  if(tabulator) {
2024
- tabulator.destroy();
2024
+ yui_tabulator_close_editor(tabulator, gobj_short_name(gobj));
2025
+ try {
2026
+ tabulator.destroy();
2027
+ } catch(e) {
2028
+ log_warning(`${gobj_short_name(gobj)}: cannot destroy the table: ${e}`);
2029
+ }
2025
2030
  gobj_write_attr(gobj, "tabulator", null);
2026
2031
  }
2027
2032
  }
@@ -3840,6 +3845,7 @@ function ac_load_nodes(gobj, event, kw, src)
3840
3845
  * Guard: defer until tableBuilt fires if Tabulator isn't ready yet.
3841
3846
  */
3842
3847
  if(tabulator._ready) {
3848
+ yui_tabulator_close_editor(tabulator, gobj_short_name(gobj));
3843
3849
  tabulator.setData(data);
3844
3850
  } else {
3845
3851
  tabulator._pendingData = data;
@@ -3919,8 +3925,18 @@ function ac_load_node_updated(gobj, event, kw, src)
3919
3925
  * it is not news: its rows are read when the topic is shown. A
3920
3926
  * row missing from a table that IS loaded is.
3921
3927
  */
3922
- if(tabulator.getRow(record.id)) {
3923
- tabulator.updateData([record]);
3928
+ let row = tabulator.getRow(record.id);
3929
+ if(row) {
3930
+ /* The echo of the reader's own write lands while the
3931
+ * next cell's editor is open: Tabulator 6.6.0 throws
3932
+ * updating a row with an open editor, and paints half of it. */
3933
+ let $row = row.getElement();
3934
+ if($row && $row.querySelector(".tabulator-editing")) {
3935
+ yui_tabulator_close_editor(tabulator, gobj_short_name(gobj));
3936
+ }
3937
+ tabulator.updateData([record]).catch((e) => {
3938
+ log_error(`${gobj_short_name(gobj)}: cannot update row '${record.id}': ${e}`);
3939
+ });
3924
3940
  } else if(tabulator.getDataCount() > 0) {
3925
3941
  log_error(`${gobj_short_name(gobj)}: updated node is not in ` +
3926
3942
  `the loaded table: ${record.id}`);
@@ -4980,6 +4996,7 @@ function ac_repull_page(gobj, event, kw, src)
4980
4996
  if(!tabulator) {
4981
4997
  return 0;
4982
4998
  }
4999
+ yui_tabulator_close_editor(tabulator, gobj_short_name(gobj));
4983
5000
  try {
4984
5001
  tabulator.replaceData();
4985
5002
  } catch(e) {
@@ -1,7 +1,7 @@
1
1
  /***********************************************************************
2
2
  * fmt_date_time.test.js
3
3
  *
4
- * fmt_date_time() of yui_time.js: a moment as a view shows it as
4
+ * fmt_date_time() of yui_date_text.js: a moment as a view shows it as
5
5
  * text -- day, short month, year, and a clock of 24 hours with its
6
6
  * seconds -- in the language it is given (the app's), and the two
7
7
  * readers built on it: time_value_to_text() (the time cells of the
@@ -13,7 +13,7 @@
13
13
  ***********************************************************************/
14
14
  import {describe, test, expect} from "vitest";
15
15
  import {set_log_callback} from "@yuneta/gobj-js";
16
- import {fmt_date_time, safe_locale} from "./yui_time.js";
16
+ import {fmt_date_time, safe_locale} from "./yui_date_text.js";
17
17
  import {time_value_to_text} from "./form_time_value.js";
18
18
  import {format_epoch} from "./json_view_helpers.js";
19
19
  import {yui_uplot_fmt_date} from "./yui_uplot_time.js";
@@ -86,6 +86,11 @@ function errors()
86
86
  return logged.filter(l => /error/i.test(l.level) || /ERROR/.test(l.msg));
87
87
  }
88
88
 
89
+ function warnings()
90
+ {
91
+ return logged.filter(l => /warn/i.test(l.level) || /WARNING/.test(l.msg));
92
+ }
93
+
89
94
  function box(form, name)
90
95
  {
91
96
  const $container = gobj_read_attr(form, "$container");
@@ -114,6 +119,8 @@ describe("the time field of a form", () => {
114
119
  expect(saved[0].as_text).toBe(ISO);
115
120
  expect(saved[0].no_time).toBe(null);
116
121
  expect(errors()).toEqual([]);
122
+ /* A column that holds no time loses nothing: nothing to say. */
123
+ expect(warnings()).toEqual([]);
117
124
  });
118
125
 
119
126
  /* The template grammar defaults a spec with no real type to
@@ -331,6 +338,11 @@ describe("a stored value the box cannot show", () => {
331
338
 
332
339
  expect(box(form, "as_epoch").value).toBe("");
333
340
  expect(box(form, "as_text").value).toBe("");
341
+ /* ...but the loss is said when it is loaded, not left silent. */
342
+ const said = warnings().map((l) => l.msg);
343
+ expect(said.length).toBe(2);
344
+ expect(said[0]).toMatch(/'as_epoch' holds "rubbish", which the time box cannot show: a save writes it as null/);
345
+ expect(said[1]).toMatch(/'as_text' holds "rubbish"/);
334
346
  gobj_send_event(form, "EV_SAVE_RECORD", {}, form);
335
347
  expect(saved[0].as_epoch).toBe(null);
336
348
  expect(saved[0].as_text).toBe(null);
@@ -112,7 +112,7 @@ function epoch_to_time_value(epoch, real_type)
112
112
  /************************************************************
113
113
  * The value of a time column as a table cell shows it: the
114
114
  * local clock, 24 hours, in the APP's language `locale`
115
- * (fmt_date_time() of yui_time.js: es "14 oct 2026, 15:05:09").
115
+ * (fmt_date_time() of yui_date_text.js: es "14 oct 2026, 15:05:09").
116
116
  * Up to gobj-ui 7.26.51 the form wrote "2026-10-14 15:05:09" and
117
117
  * the treedb table "14 oct 2026, 03:05:09 p. m.", in the
118
118
  * browser's language. "" for no time; a value that is not one is
@@ -217,7 +217,7 @@ export {TIME_FIELDS};
217
217
  /************************************************************
218
218
  * Format an epoch value as a local wall-clock string, 24
219
219
  * hours, in the APP's language `locale` (fmt_date_time() of
220
- * yui_time.js, as the time cells of the forms and tables;
220
+ * yui_date_text.js, as the time cells of the forms and tables;
221
221
  * up to gobj-ui 7.26.51 it was toLocaleString(): the
222
222
  * browser's language, and 12 hours in English).
223
223
  * Accepts seconds or milliseconds; returns null when the
@@ -1,14 +1,15 @@
1
1
  /***********************************************************************
2
2
  * tabulator_edit_internals.test.js
3
3
  *
4
- * C_YUI_FORM closes a table's open cell editor before it wipes
5
- * the rows (close_table_editor): Tabulator 6.6.0 closes it while
4
+ * C_YUI_FORM and C_YUI_TREEDB_TOPIC_WITH_FORM close a table's
5
+ * open cell editor before they wipe the rows
6
+ * (yui_tabulator_close_editor): Tabulator 6.6.0 closes it while
6
7
  * it wipes them and throws. Tabulator has no public way to ask
7
- * which cell is being edited, so the form reads its edit module:
8
+ * which cell is being edited, so the helper reads its edit module:
8
9
  * `table.modules.edit`, its `currentCell` and its `cancelEdit()`.
9
10
  * This pins those names against the Tabulator INSTALLED: an
10
11
  * upgrade that renames them fails here, not in a reader's
11
- * reload. (The form also says so in the log when they are
12
+ * reload. (The helper also says so in the log when they are
12
13
  * missing.)
13
14
  *
14
15
  * Copyright (c) 2026, ArtGins.
@@ -0,0 +1,173 @@
1
+ /***********************************************************************
2
+ * treedb_topic_editor_guard.wiring.test.js
3
+ *
4
+ * C_YUI_TREEDB_TOPIC_WITH_FORM cancels an open cell editor before
5
+ * each call of its table that would throw under it in Tabulator
6
+ * 6.6.0: setData (EV_LOAD_NODES), replaceData (EV_REPULL_PAGE),
7
+ * destroy (mt_stop), and updateData (EV_LOAD_NODE_UPDATED) of the
8
+ * row being edited -- the backend echo of a write the reader just
9
+ * made, which arrives while the next cell's editor is open. A
10
+ * refused updateData is logged, not left unhandled. The table is a
11
+ * double that records the order of the calls; that the real
12
+ * Tabulator throws without this is proved in a browser by
13
+ * scripts/qa-cell-editor.mjs.
14
+ *
15
+ * Copyright (c) 2026, ArtGins.
16
+ * All Rights Reserved.
17
+ ***********************************************************************/
18
+ import {describe, test, expect, beforeAll, beforeEach, vi} from "vitest";
19
+ import {install_dom_double} from "../test/dom_double.js";
20
+
21
+ install_dom_double();
22
+
23
+ if(typeof globalThis.ResizeObserver === "undefined") {
24
+ globalThis.ResizeObserver = class {
25
+ observe() {
26
+ }
27
+ unobserve() {
28
+ }
29
+ disconnect() {
30
+ }
31
+ };
32
+ }
33
+
34
+ const state = vi.hoisted(() => ({tables: [], calls: [], edited_row: null, refuse: false}));
35
+ vi.mock("tabulator-tables", () => {
36
+ class FakeTabulator {
37
+ constructor($el, settings) {
38
+ this.element = $el;
39
+ this.options = settings;
40
+ this.modules = {edit: {
41
+ get currentCell() {
42
+ return state.edited_row? {row: state.edited_row} : false;
43
+ },
44
+ cancelEdit() {
45
+ state.calls.push(`cancel ${state.edited_row}`);
46
+ state.edited_row = null;
47
+ },
48
+ }};
49
+ state.tables.push(this);
50
+ return new Proxy(this, {
51
+ get(target, prop) {
52
+ if(prop in target) {
53
+ return target[prop];
54
+ }
55
+ return () => [];
56
+ }
57
+ });
58
+ }
59
+ on() {
60
+ }
61
+ off() {
62
+ }
63
+ getRow(id) {
64
+ return {getElement: () => ({
65
+ querySelector: (s) => (s === ".tabulator-editing" && state.edited_row === id)? {} : null,
66
+ })};
67
+ }
68
+ getDataCount() {
69
+ return 2;
70
+ }
71
+ setData() {
72
+ state.calls.push(`setData edited=${state.edited_row}`);
73
+ }
74
+ replaceData() {
75
+ state.calls.push(`replaceData edited=${state.edited_row}`);
76
+ }
77
+ updateData(records) {
78
+ state.calls.push(`updateData ${records[0].id} edited=${state.edited_row}`);
79
+ return state.refuse? Promise.reject("Update Error - Unable to update row") : Promise.resolve();
80
+ }
81
+ destroy() {
82
+ state.calls.push(`destroy edited=${state.edited_row}`);
83
+ }
84
+ }
85
+ return {TabulatorFull: FakeTabulator, Tabulator: FakeTabulator};
86
+ });
87
+
88
+ const {
89
+ SDATA_END, gclass_create, set_log_callback, gobj_send_event, gobj_stop,
90
+ gobj_start_up, gobj_create_yuno, gobj_create_service, gobj_start,
91
+ } = await import("@yuneta/gobj-js");
92
+ const {register_c_yui_treedb_topic_with_form} = await import("./c_yui_treedb_topic_with_form.js");
93
+
94
+ const logged = [];
95
+ let yuno = null;
96
+ let n = 0;
97
+
98
+ beforeAll(async () => {
99
+ const i18next = (await import("i18next")).default;
100
+ await i18next.init({lng: "en", resources: {}});
101
+ gobj_start_up(null, null, null, null, null, null, null);
102
+ set_log_callback((level, msg) => {
103
+ logged.push({level: String(level), msg: String(msg)});
104
+ });
105
+ gclass_create("C_TEST_EDITOR_GUARD_HOST", [], [["ST_IDLE", []]], {}, 0, [SDATA_END()], {}, 0, 0, 0, 0);
106
+ register_c_yui_treedb_topic_with_form();
107
+ yuno = gobj_create_yuno("editor_guard_yuno", "C_TEST_EDITOR_GUARD_HOST", {});
108
+ gobj_start(yuno);
109
+ });
110
+
111
+ beforeEach(() => {
112
+ state.calls.length = 0;
113
+ state.edited_row = null;
114
+ state.refuse = false;
115
+ logged.length = 0;
116
+ });
117
+
118
+ function make_view()
119
+ {
120
+ const view = gobj_create_service(`editor_guard_view_${++n}`, "C_YUI_TREEDB_TOPIC_WITH_FORM", {
121
+ treedb_name: "treedb_test",
122
+ topic_name: "people",
123
+ desc: {id: "people", pkey: "id", cols: [
124
+ {id: "id", header: "Id", type: "string", flag: ["persistent", "required"]},
125
+ {id: "name", header: "name", type: "string", flag: ["persistent", "writable"]},
126
+ ]}
127
+ }, yuno);
128
+ gobj_start(view);
129
+ state.tables[state.tables.length - 1]._ready = true;
130
+ return view;
131
+ }
132
+
133
+ describe("an open cell editor of a treedb topic's table", () => {
134
+ test("is cancelled before setData (EV_LOAD_NODES)", () => {
135
+ const view = make_view();
136
+ state.edited_row = "b";
137
+ gobj_send_event(view, "EV_LOAD_NODES", [{id: "x"}], view);
138
+ expect(state.calls).toEqual(["cancel b", "setData edited=null"]);
139
+ });
140
+
141
+ test("is cancelled before replaceData (EV_REPULL_PAGE)", () => {
142
+ const view = make_view();
143
+ state.edited_row = "b";
144
+ gobj_send_event(view, "EV_REPULL_PAGE", {}, view);
145
+ expect(state.calls).toEqual(["cancel b", "replaceData edited=null"]);
146
+ });
147
+
148
+ test("is cancelled before the table is destroyed (stop)", () => {
149
+ const view = make_view();
150
+ state.edited_row = "b";
151
+ gobj_stop(view);
152
+ expect(state.calls).toEqual(["cancel b", "destroy edited=null"]);
153
+ });
154
+
155
+ test("is cancelled before updateData of ITS row, left alone for another row", () => {
156
+ const view = make_view();
157
+ state.edited_row = "b";
158
+ gobj_send_event(view, "EV_LOAD_NODE_UPDATED", [{id: "a", name: "A"}], view);
159
+ gobj_send_event(view, "EV_LOAD_NODE_UPDATED", [{id: "b", name: "B"}], view);
160
+ expect(state.calls).toEqual([
161
+ "updateData a edited=b",
162
+ "cancel b", "updateData b edited=null",
163
+ ]);
164
+ });
165
+
166
+ test("a refused updateData is logged as an error, not left unhandled", async () => {
167
+ const view = make_view();
168
+ state.refuse = true;
169
+ gobj_send_event(view, "EV_LOAD_NODE_UPDATED", [{id: "a", name: "A"}], view);
170
+ await new Promise((resolve) => setTimeout(resolve, 0));
171
+ expect(logged.filter((l) => /cannot update row 'a'.*Unable to update row/.test(l.msg)).length).toBe(1);
172
+ });
173
+ });
@@ -18,24 +18,65 @@
18
18
  * (form_time_cell_editor.js) or Tabulator's own.
19
19
  *
20
20
  * `setColumns()` (a language change of
21
- * C_YUI_TREEDB_TOPIC_WITH_FORM runs it) and `setHeight`,
22
- * `setMaxHeight` and `setMinHeight` build the cells again the
23
- * same way, and Tabulator throws the same.
21
+ * C_YUI_TREEDB_TOPIC_WITH_FORM runs it), `setHeight`,
22
+ * `setMaxHeight`, `setMinHeight`, `addColumn`,
23
+ * `updateColumnDefinition` and `moveColumn` build the cells
24
+ * again the same way, and Tabulator throws the same.
24
25
  *
25
26
  * So the edit is ended BEFORE each of those calls, while its cell
26
27
  * is alive, and opened again on the new cell of the same row
27
- * and column after it. What was typed in the box is lost; the
28
- * editor stays open under the reader.
28
+ * and column after it (after the promise, for the calls that
29
+ * answer one). What was typed in the box is lost; the editor
30
+ * stays open under the reader. It is NOT opened again, and the
31
+ * log says why, when: the cell is out of the page (a row the
32
+ * virtual DOM does not render, or a filter hides), its column is
33
+ * gone or has no editor any more, the call answered a REFUSAL
34
+ * (it rebuilt nothing), or -- before a promise answers -- the
35
+ * table was destroyed or another cell's editor was opened.
36
+ *
37
+ * A call that WIPES the rows (setData, replaceData, clearData,
38
+ * destroy) cancels the edit first with
39
+ * `yui_tabulator_close_editor()`: there is no cell to open it
40
+ * on again.
29
41
  *
30
42
  * Copyright (c) 2026, ArtGins.
31
43
  * All Rights Reserved.
32
44
  ****************************************************************************/
33
45
  import {log_warning} from "@yuneta/gobj-js";
34
46
 
47
+ /************************************************************
48
+ * Close the cell editor a table has open, dropping what is
49
+ * typed in it, before its rows are wiped (setData, replaceData,
50
+ * clearData, destroy). Tabulator 6.6.0 closes it while it wipes
51
+ * them, and the redraw that follows walks the rows already
52
+ * wiped: with an editor open in any row but the first it threw
53
+ * ("reading 'add'"), and left the table half wiped.
54
+ *
55
+ * Tabulator has no public way to ask which cell is edited: this
56
+ * reads its edit module (`modules.edit`, `currentCell`,
57
+ * `cancelEdit()`), names pinned against the installed Tabulator
58
+ * by tabulator_edit_internals.test.js. A Tabulator without them
59
+ * is said: the guard would do nothing, and the crash come back.
60
+ * `who` names the owner in that warning.
61
+ ************************************************************/
62
+ function yui_tabulator_close_editor(tabulator, who)
63
+ {
64
+ let edit = tabulator.modules? tabulator.modules.edit : null;
65
+ if(!edit || !("currentCell" in edit) || typeof edit.cancelEdit !== "function") {
66
+ log_warning(`${who}: this Tabulator has no modules.edit.currentCell/cancelEdit(): ` +
67
+ `an open cell editor is not closed before its table is wiped`);
68
+ return;
69
+ }
70
+ if(edit.currentCell) {
71
+ edit.cancelEdit();
72
+ }
73
+ }
74
+
35
75
  /************************************************************
36
76
  * Wrap the calls of `tabulator` that build the cells again --
37
- * a forced `redraw`, `setColumns` and the three height setters
38
- * -- so that each ends the open edit first and opens it again
77
+ * a forced `redraw`, `setColumns`, the three height setters
78
+ * and `addColumn`, `updateColumnDefinition`, `moveColumn` --
79
+ * so that each ends the open edit first and opens it again
39
80
  * after.
40
81
  ************************************************************/
41
82
  function yui_tabulator_keep_edit_on_redraw(tabulator)
@@ -51,6 +92,54 @@ function yui_tabulator_keep_edit_on_redraw(tabulator)
51
92
  editing = null;
52
93
  });
53
94
 
95
+ function stays_closed(field, why)
96
+ {
97
+ log_warning(`yui_tabulator_keep_edit_on_redraw: the edit of '${field}' stays closed: ${why}`);
98
+ }
99
+
100
+ function has_column(field)
101
+ {
102
+ return tabulator.getColumns().some((c) => c.getField() === field);
103
+ }
104
+
105
+ function edit_again(row, field, name)
106
+ {
107
+ if(tabulator.destroyed) {
108
+ stays_closed(field, `the table was destroyed after ${name}()`);
109
+ return;
110
+ }
111
+ let $open = editing? editing.getElement() : null;
112
+ if($open && $open.classList.contains("tabulator-editing")) {
113
+ stays_closed(field, `another cell ('${editing.getField()}') was opened after ${name}()`);
114
+ return;
115
+ }
116
+ /* A row the page does not draw keeps the cells of the old
117
+ * columns, so it answers no cell for a column that does exist. */
118
+ let again = row.getCell(field);
119
+ if(!again) {
120
+ if(has_column(field)) {
121
+ stays_closed(field, `its row is not in the page after ${name}()`);
122
+ } else {
123
+ stays_closed(field, `no column '${field}' after ${name}()`);
124
+ }
125
+ return;
126
+ }
127
+ let $again = again.getElement();
128
+ if(!$again || !$again.isConnected) {
129
+ stays_closed(field, `its row is not in the page after ${name}()`);
130
+ return;
131
+ }
132
+ if(!again.getColumn().getDefinition().editor) {
133
+ stays_closed(field, `the column has no editor after ${name}()`);
134
+ return;
135
+ }
136
+ try {
137
+ again.edit(true);
138
+ } catch(e) {
139
+ stays_closed(field, `Tabulator refused to open it after ${name}(): ${e}`);
140
+ }
141
+ }
142
+
54
143
  function keep_edit_around(name, rebuilds)
55
144
  {
56
145
  let original = tabulator[name].bind(tabulator);
@@ -66,20 +155,30 @@ function yui_tabulator_keep_edit_on_redraw(tabulator)
66
155
  let field = cell.getField();
67
156
  cell.cancelEdit();
68
157
  let ret = original(...args);
69
- let again = row.getCell(field);
70
- if(!again) {
71
- log_warning(`yui_tabulator_keep_edit_on_redraw: no cell '${field}' after ${name}()`);
72
- return ret;
158
+ if(ret && typeof ret.then === "function") {
159
+ return ret.then(
160
+ (value) => {
161
+ edit_again(row, field, name);
162
+ return value;
163
+ },
164
+ (reason) => {
165
+ /* The edit had to be ended before asking; a refused
166
+ * call rebuilt nothing, so nothing is opened again. */
167
+ stays_closed(field, `${name}() was refused: ${reason}`);
168
+ throw reason;
169
+ }
170
+ );
73
171
  }
74
- again.edit(true);
172
+ edit_again(row, field, name);
75
173
  return ret;
76
174
  };
77
175
  }
78
176
 
79
177
  keep_edit_around("redraw", (args) => !!args[0]);
80
- for(const name of ["setColumns", "setHeight", "setMaxHeight", "setMinHeight"]) {
178
+ for(const name of ["setColumns", "setHeight", "setMaxHeight", "setMinHeight",
179
+ "addColumn", "updateColumnDefinition", "moveColumn"]) {
81
180
  keep_edit_around(name, () => true);
82
181
  }
83
182
  }
84
183
 
85
- export {yui_tabulator_keep_edit_on_redraw};
184
+ export {yui_tabulator_keep_edit_on_redraw, yui_tabulator_close_editor};