scope: continue implementation, adjust clone target acquire mechanism, add scoped linkId

This commit is contained in:
jean-emmanuel 2026-08-13 17:50:57 +02:00
parent 0e8c77a700
commit 08b837ff98
5 changed files with 81 additions and 78 deletions

View file

@ -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
}
}

View file

@ -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<this.idPrefix.length; i++) {
this.idPrefix[i] = this.idPrefix.slice(i).reverse().join('/')
for (var i=0; i < this.scopes.length; i++) {
this.scopes[i] = this.scopes.slice(i).reverse().join('/')
}
this.idPrefix.push('')
}
// @{props} links lists
@ -547,14 +547,13 @@ class Widget extends EventEmitter {
if (widget === this) id = 'this'
/*
scopable: attempt to resolve widget updates against scoped ids
scopable: attempt to resolve scoped widget updates against ids in advanced syntax blocks
*/
if (this.meta.scopable) {
var unprefixedId
for (var p of this.idPrefix) {
if (p === '') continue
for (var p of this.scopes) {
if (id.includes(p)) {
unprefixedId = id.replace(p, '')
unprefixedId = id.replace(p + '/', '')
if (this.nestedLinkedProps[unprefixedId] || linkedProps[unprefixedId]) {
id = unprefixedId
break
@ -653,7 +652,7 @@ class Widget extends EventEmitter {
var widgets = id == 'parent' && this.parent ?
[this.parent] : id == 'this' ? [this] :
widgetManager.getWidgetById(id, this.idPrefix)
widgetManager.getWidgetById(id, this.scopes)
if (!widgets.length) {
var parent = this.parent
@ -953,13 +952,12 @@ class Widget extends EventEmitter {
transformProp(propName, propValue) {
switch (propName) {
/*
scopable: prefix id with the closest (most restrictive) scope
*/
case 'id':
case 'linkId':
/*
scopable: prefix id & linkId
*/
if (this.meta.scopable) {
propValue = this.idPrefix[0] + propValue
if (this.scopes.length) {
propValue = this.scopes[0] + '/' + propValue
}
break
}

View file

@ -25,7 +25,7 @@ class Clone extends Container() {
widgetId: {type: 'string', value: '', help: '`id` of the widget to clone'},
scopedId: {type: 'boolean|string', value: false, help: [
'Set to `true` to prefix all cloned `id`s inside the clone with a unique identifier and make the clone independent from its original. All `@{}` blocks and scripts will be prefixed automatically so that they resolve to widgets within the clone if possible, falling back to unprefixed ids otherwise. `linkId` will be prefixed as well but will only resolve within the clone.',
'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: 'Cloned widget\'s properties to override'},
},
@ -51,9 +51,10 @@ class Clone extends Container() {
this.cloneLock = false
this.cloneTarget = null
this.targetRank = null
this.cloneClass = []
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
@ -68,8 +69,7 @@ class Clone extends Container() {
parent = parent.parent
}
this.bindTarget(this.getCloneTarget())
if (this.cloneTarget) this.createClone(true)
this.getCloneTarget()
// global listener to catch cloneTarget's creation if no target is locked
widgetManager.on('widget-created', (e)=>{
@ -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()

View file

@ -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

View file

@ -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)
}