fragment: add fallback property

This commit is contained in:
jean-emmanuel 2022-11-09 10:14:54 +01:00
parent c7ce398632
commit 2552dd187e
4 changed files with 43 additions and 5 deletions

View file

@ -18,6 +18,8 @@
- add `reload()` function
- expose Navigator object with `getNavigator()`
- widgets
- fragment: add `fallback` property
## 1.19.2

View file

@ -231,9 +231,14 @@ var SessionManager = class SessionManager extends EventEmitter {
setFragment(data) {
var {path, fileContent, raw} = data,
var {path, fileContent, raw, notFound} = data,
update = false
if (notFound) {
this.trigger('fragment-not-found', {path: path})
return
}
if (raw) {
if (this.fragments[path] !== fileContent) {
this.fragments[path] = fileContent

View file

@ -25,6 +25,7 @@ class Fragment extends Container() {
class_specific: {
file: {type: 'string', value: '', help: 'Fragment file path (relative to the session or theme file location by default, falling back to absolute path)'},
fallback: {type: 'string', value: '', help: 'Fallack fragment file path, loaded if `file` can\'t be opened'},
props: {type: 'object', value: {}, editor: 'javascript', syntaxChecker: false, help: 'Fragment widget\'s properties to override'},
},
value: null,
@ -65,11 +66,25 @@ class Fragment extends Container() {
sessionManager.on('fragment-updated', (e)=>{
var {path} = e
if (path === this.getProp('file')) {
if (path === this.getProp('file') || path === this.getProp('fallback')) {
this.createFragment()
}
}, {context: this})
sessionManager.on('fragment-not-found', (e)=>{
var {path} = e
if (path === this.getProp('file')) {
if (this.getProp('fallback')) {
if (this.getFragment()) this.createFragment()
else sessionManager.loadFragment(this.getProp('fallback'))
} else {
this.log(`fragment file not found`)
}
} else if (path === this.getProp('fallback') && !this.getFragment()) {
this.log(`fallback fragment file not found`)
}
}, {context: this})
if (this.getProp('file')) {
if (sessionManager.getFragment(this.getProp('file'))) {
this.createFragment(true)
@ -81,10 +96,22 @@ class Fragment extends Container() {
}
createFragment(init) {
getFragment() {
var fragment = sessionManager.getFragment(this.getProp('file'))
if (!fragment && this.getProp('fallback')) {
fragment = sessionManager.getFragment(this.getProp('fallback'))
}
return fragment
}
createFragment(init) {
var fragment = this.getFragment()
if (!fragment) return
@ -139,7 +166,7 @@ class Fragment extends Container() {
updateFragment() {
var fragment = sessionManager.getFragment(this.getProp('file'))
var fragment = this.getFragment()
if (!fragment) return

View file

@ -19,6 +19,7 @@ class FragmentManager {
then(result)
}, (error)=>{
ipc.send('errorLog', `Could not open fragment file:\n ${error}`, clientId)
ipc.send('fragmentLoad', {path: path, notFound: true})
this.deleteFragment(path)
})
@ -29,7 +30,10 @@ class FragmentManager {
var path = data.path
var resolvedPath = resolvePath(path, clientId)
if (!resolvedPath) return ipc.send('errorLog', `Fragment file not found: ${path}`, clientId)
if (!resolvedPath) {
ipc.send('fragmentLoad', {path: path, notFound: true})
return
}
if (!this.clients[resolvedPath]) this.clients[resolvedPath] = []
if (!this.clients[resolvedPath].includes(clientId)) this.clients[resolvedPath].push(clientId)