From de1639702db449ae623d7bf182236e6a40755509 Mon Sep 17 00:00:00 2001 From: hizzgdev Date: Mon, 4 Dec 2023 01:17:58 +0800 Subject: [PATCH 1/2] enable show mind in hidden container --- src/jsmind.view_provider.js | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/jsmind.view_provider.js b/src/jsmind.view_provider.js index d3761213..fbc2e4c1 100644 --- a/src/jsmind.view_provider.js +++ b/src/jsmind.view_provider.js @@ -68,7 +68,17 @@ export class ViewProvider { }); this.container.appendChild(this.e_panel); + + if (!this.container.offsetParent) { + new IntersectionObserver((entities, observer) => { + if (entities[0].isIntersecting) { + observer.unobserve(this.e_panel); + this.resize() + } + }).observe(this.e_panel); + } } + add_event(obj, event_name, event_handle, capture_by_panel) { let target = !!capture_by_panel ? this.e_panel : this.e_nodes; $.on(target, event_name, function (e) { @@ -159,13 +169,32 @@ export class ViewProvider { this.create_node_element(nodes[nodeid], doc_frag); } this.e_nodes.appendChild(doc_frag); - for (var nodeid in nodes) { - this.init_nodes_size(nodes[nodeid]); - } + + this.run_in_c11y_mode_if_needed(() => { + for (var nodeid in nodes) { + this.init_nodes_size(nodes[nodeid]); + } + }); } add_node(node) { this.create_node_element(node, this.e_nodes); - this.init_nodes_size(node); + this.run_in_c11y_mode_if_needed(() => { + this.init_nodes_size(node); + }); + } + run_in_c11y_mode_if_needed(func) { + if (!!this.container.offsetParent) { + func(); + return; + } + logger.warn('init nodes in compatibility mode. because the container or its parent has style {display:none}. ') + this.e_panel.style.position = 'absolute'; + this.e_panel.style.top = '-100000'; + $.d.body.appendChild(this.e_panel); + func(); + this.container.appendChild(this.e_panel); + this.e_panel.style.position = null; + this.e_panel.style.top = null; } create_node_element(node, parent_node) { var view_data = null; From b446aac45f903dcaf4b9dcab8804026efacf543f Mon Sep 17 00:00:00 2001 From: hizzgdev Date: Mon, 4 Dec 2023 01:31:19 +0800 Subject: [PATCH 2/2] fix format and UT --- src/jsmind.view_provider.js | 6 ++++-- tests/unit/jsmind.test.js | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/jsmind.view_provider.js b/src/jsmind.view_provider.js index fbc2e4c1..d7cf9447 100644 --- a/src/jsmind.view_provider.js +++ b/src/jsmind.view_provider.js @@ -73,7 +73,7 @@ export class ViewProvider { new IntersectionObserver((entities, observer) => { if (entities[0].isIntersecting) { observer.unobserve(this.e_panel); - this.resize() + this.resize(); } }).observe(this.e_panel); } @@ -187,7 +187,9 @@ export class ViewProvider { func(); return; } - logger.warn('init nodes in compatibility mode. because the container or its parent has style {display:none}. ') + logger.warn( + 'init nodes in compatibility mode. because the container or its parent has style {display:none}. ' + ); this.e_panel.style.position = 'absolute'; this.e_panel.style.top = '-100000'; $.d.body.appendChild(this.e_panel); diff --git a/tests/unit/jsmind.test.js b/tests/unit/jsmind.test.js index 4399caef..9a5547af 100644 --- a/tests/unit/jsmind.test.js +++ b/tests/unit/jsmind.test.js @@ -10,6 +10,13 @@ beforeAll(() => { logger.error = jest.fn(); logger.warn = jest.fn(); logger.debug = jest.fn(); + + const observe = jest.fn(); + const unobserve = jest.fn(); + window.IntersectionObserver = jest.fn(() => ({ + observe, + unobserve, + })); }); const mockElement = {