From 08b837ff98c2448ed8c90f52c0d787fb49ad7667 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 13 Aug 2026 17:50:57 +0200 Subject: [PATCH] scope: continue implementation, adjust clone target acquire mechanism, add scoped linkId --- src/client/managers/widgets.mjs | 9 +- src/client/widgets/common/widget.mjs | 32 +++--- src/client/widgets/containers/clone.mjs | 112 ++++++++++----------- src/client/widgets/containers/fragment.mjs | 4 +- src/client/widgets/scripts/script-vm.mjs | 2 +- 5 files changed, 81 insertions(+), 78 deletions(-) diff --git a/src/client/managers/widgets.mjs b/src/client/managers/widgets.mjs index 1241c746..16bc9563 100644 --- a/src/client/managers/widgets.mjs +++ b/src/client/managers/widgets.mjs @@ -38,6 +38,8 @@ class WidgetManager extends EventEmitter { if (linkId) { if (!Array.isArray(linkId)) linkId = [linkId] linkId = linkId.map(x=>String(x).replace(/^>>\s*/, '')).filter(x=>x.indexOf('<<') < 0) + // scopable; prefix linkId with the closest (most restrictive) scope + if (widget.scopes.length) linkId = linkId.map(x => x + widget.scopes[0]) linkId = linkId.filter(x=>this.linkIdLock.indexOf(x)<0) this.linkIdLock = this.linkIdLock.concat(linkId) this.linkIdLockDepth++ @@ -117,6 +119,8 @@ class WidgetManager extends EventEmitter { if (linkId) { if (!Array.isArray(linkId)) linkId = [linkId] linkId = linkId.map(x=>String(x).replace(/^<<\s*/, '')).filter(x=>x.indexOf('>>') < 0) + // scopable; prefix linkId with the closest (most restrictive) scope + if (widget.scopes.length) linkId = linkId.map(x => x + widget.scopes[0]) for (var i in linkId) { if (linkId[i]) { if (!this.linkIdRoute[linkId[i]]) this.linkIdRoute[linkId[i]] = [] @@ -181,6 +185,8 @@ class WidgetManager extends EventEmitter { if (!Array.isArray(linkId)) linkId = [linkId] linkId = linkId.map(x=>String(x).replace(/^<<\s*/, '')).filter(x=>x.indexOf('>>') < 0) + // scopable; prefix linkId with the closest (most restrictive) scope + if (widget.scopes.length) linkId = linkId.map(x => x + widget.scopes[0]) for (var i in linkId) { if (linkId[i] && this.linkIdRoute[linkId[i]] && this.linkIdRoute[linkId[i]].indexOf(hash) != -1) this.linkIdRoute[linkId[i]].splice(this.linkIdRoute[linkId[i]].indexOf(hash), 1) } @@ -263,14 +269,13 @@ class WidgetManager extends EventEmitter { getWidgetById(id, scope) { /* - scopable: cache id prefixes (clone scopes), from neaest to farthest + scopable: return matching widgets only for the closest scope with results */ if (scope) { for (var prefix of scope) { var prefixedId = Array.isArray(id) ? id.map(i=>prefix + i) : prefix + id var widgets = this.getWidgetBy(prefixedId, this.idRoute) - // return matching widgets for the first matching scope if (widgets.length) return widgets } } diff --git a/src/client/widgets/common/widget.mjs b/src/client/widgets/common/widget.mjs index 24005fce..0c89f166 100644 --- a/src/client/widgets/common/widget.mjs +++ b/src/client/widgets/common/widget.mjs @@ -193,6 +193,7 @@ class Widget extends EventEmitter { this.removed = false this.mounted = false this.visible = true + this.scopes = [] if (options.value !== undefined) this.value = options.value @@ -418,21 +419,20 @@ class Widget extends EventEmitter { createPropsCache() { /* - scopable: cache id prefixes (clone scopes), from neaest to farthest + scopable: cache id prefixes (scopes), from nearest to farthest */ - this.idPrefix = [] + this.scopes = [] if (this.meta.scopable) { var parent = this.parent do { if (parent.getProp('scopedId')) { - this.idPrefix.push(parent.scopeId) + this.scopes.push(parent.scopeId) } parent = parent.parent } while (parent.meta.scopable || parent.getProp('scopedId')) - for (var i=0; i{ @@ -78,24 +78,9 @@ class Clone extends Container() { var {id, widget} = e - if (id === this.getProp('widgetId') && this.isValidCloneTarget(widget)) { + if (id.includes(this.getProp('widgetId')) && this.isValidCloneTarget(widget)) { - var clone = this.children[0] - - this.unbindTarget() - - if (!clone) { - - this.updateContainer(false) - this.bindTarget(widget) - this.createClone() - - } else { - - this.bindTarget(widget) - this.updateClone() - - } + this.getCloneTarget() } @@ -129,32 +114,52 @@ class Clone extends Container() { // attempt to acquire a cloneTarget if 'widget-created' event has not been caught // (the target might have been created before the clone itself) - var widgets = widgetManager.getWidgetById(this.getProp('widgetId')) - .filter(el=>this.isValidCloneTarget(el)) + var scopes = this.prefixId || [''], + target = null, + rank = 0 - if (widgets.length === 0) return null - if (widgets.length === 1) { - return widgets[0] - } + for (var scope of scopes) { + var widgets = widgetManager.getWidgetById(this.getProp('widgetId'), scope) + .filter(el=>this.isValidCloneTarget(el)) + target = widgets[0] - // when duplicating clones pointing to an object with a static id, - // each clone will recreate an object with the same id internally - // we try to get the original one if there is more than 2 clone pointing to the same id (avoiding cloning the clone) + if (!target) continue - function isCreatedByAClone(wi){ - var parent = wi.parent - return parent && parent.getProp('type') === 'clone' - } - - var target = widgets[0] - for (var w of widgets) { - if (!isCreatedByAClone(w)) { - target = w - break + for (var w of widgets) { + // when duplicating clones pointing to an object with a static id, + // each clone will recreate an object with the same id internally + // we try to get the original one if there is more than 2 clone pointing to the same id (avoiding cloning the clone) + if (!(w.parent && w.parent.getProp('type') == 'clone')) { + target = w + break + } } - } - return target + if (target && (rank < this.targetRank || this.targetRank == null)) { + + var clone = this.children[0] + + this.unbindTarget() + + if (!clone) { + + this.updateContainer(false) + this.bindTarget(target, rank) + this.createClone() + + } else { + + this.bindTarget(target, rank) + this.updateClone() + + } + + return + } + + rank++ + + } } @@ -198,14 +203,16 @@ class Clone extends Container() { this.cloneTarget.off(undefined, undefined, this) this.cloneTarget = null + this.targetRank = null } - bindTarget(target) { + bindTarget(target, rank) { if (!target) return this.cloneTarget = target + this.targetRank = rank // listen for cloneTarget's deletion // if it is just edited, its recreation will be caught by the global 'widget-created' event handler @@ -218,18 +225,14 @@ class Clone extends Container() { // listen for cloneTarget's content updates this.cloneTarget.on('widget-created', (e)=>{ - var {widget} = e, - parent = widget.parent + var {widget} = e - while (parent !== this.cloneTarget && parent !== widgetManager) { - // if updated widget is in a nested clone, ignore it: - // it will be handled by the nested clone that targets it - if (parent.getProp('type') === 'clone') return - parent = parent.parent + // if updated widget is in a nested clone, ignore it: + // it will be handled by the nested clone that targets it + if (!widget.meta.clone) { + this.updateClone() } - this.updateClone() - }, {context: this}) } @@ -310,10 +313,7 @@ class Clone extends Container() { switch (propName) { case 'widgetId': - this.unbindTarget() - this.updateContainer(false) - this.bindTarget(this.getCloneTarget()) - if (this.cloneTarget) this.createClone() + this.getCloneTarget() return case 'props': if (this.children[0]) this.updateClone() diff --git a/src/client/widgets/containers/fragment.mjs b/src/client/widgets/containers/fragment.mjs index 20fa20fc..0ed04281 100644 --- a/src/client/widgets/containers/fragment.mjs +++ b/src/client/widgets/containers/fragment.mjs @@ -31,7 +31,7 @@ class Fragment extends Container() { fallback: {type: 'string', value: '', help: 'Fallack fragment file path, loaded if `file` can\'t be opened'}, scopedId: {type: 'boolean|string', value: false, help: [ 'Set to `true` to prefix all widget `id`s inside the clone with a unique identifier and make the fragment self-contained. All `@{}` blocks and scripts will be prefixed automatically so that they resolve to widgets within the fragment if possible, falling back to unprefixed ids otherwise. `linkId` will be prefixed as well but will only resolve within the fragment.', - 'Set to a string to define the `ìd` prefix manually.' + 'Set to a string to define the `ìd` prefix manually (a trailing slash will be added automatically).' ]}, props: {type: 'object', value: {}, editor: 'javascript', syntaxChecker: false, help: 'Fragment widget\'s properties to override'}, }, @@ -55,7 +55,7 @@ class Fragment extends Container() { this.noValueState = true - this.scopeId = this.getProp('scopedId') === true ? this.hash + '/' : this.getProp('scopedId') === false ? '' : this.getProp('scopedId') + this.scopeId = this.getProp('scopedId') === true ? this.hash : this.getProp('scopedId') === false ? '' : this.getProp('scopedId') this.modalBreakout = 0 diff --git a/src/client/widgets/scripts/script-vm.mjs b/src/client/widgets/scripts/script-vm.mjs index 50dbf97a..e5312e32 100644 --- a/src/client/widgets/scripts/script-vm.mjs +++ b/src/client/widgets/scripts/script-vm.mjs @@ -44,7 +44,7 @@ class ScriptVm extends Vm { if (id === 'this') return [widget] else if (id === 'parent' && widget.parent !== widgetManager) return [widget.parent] - else return widgetManager.getWidgetById(id, widget.meta.scopable ? widget.idPrefix : null) + else return widgetManager.getWidgetById(id, widget.scopes) }