fragment: wip
TODO: - server side file cache - relative path resolve - watch/autoreload
This commit is contained in:
parent
b4296d2ea5
commit
0c9d2a2e4b
11 changed files with 292 additions and 18 deletions
|
|
@ -5,7 +5,9 @@ var {updateWidget} = require('./data-workers'),
|
|||
editor = require('./'),
|
||||
locales = require('../locales'),
|
||||
ContextMenu = require('../ui/context-menu'),
|
||||
contextMenu = new ContextMenu()
|
||||
contextMenu = new ContextMenu(),
|
||||
sessionManager
|
||||
|
||||
|
||||
|
||||
var multiSelectKey = (navigator.platform || '').match('Mac') ? 'metaKey' : 'ctrlKey'
|
||||
|
|
@ -274,6 +276,21 @@ var handleClick = function(event) {
|
|||
}
|
||||
|
||||
|
||||
if (parent !== widgetManager && data.length === 1) {
|
||||
|
||||
actions.push({
|
||||
separator: true
|
||||
})
|
||||
|
||||
actions.push({
|
||||
label: icon('file-download') + ' ' + locales('editor_fragment_export'),
|
||||
action: ()=>{
|
||||
sessionManager = sessionManager || require('../managers/session')
|
||||
sessionManager.saveFragment(editor.selectedWidgets[0].props)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (parent !== widgetManager) {
|
||||
|
||||
actions.push({
|
||||
|
|
|
|||
|
|
@ -45,6 +45,12 @@ module.exports = {
|
|||
|
||||
},
|
||||
|
||||
fragmentLoad: function(data){
|
||||
|
||||
session.setFragment(data)
|
||||
|
||||
},
|
||||
|
||||
stateLoad: function(data){
|
||||
|
||||
state.load(data.state, data.send, data.path)
|
||||
|
|
@ -77,6 +83,12 @@ module.exports = {
|
|||
new UiModal({title: raw(utils.icon('exclamation-triangle') + ' ' + locales('error')), content: raw(data), closable:true})
|
||||
},
|
||||
|
||||
errorLog: function(data) {
|
||||
|
||||
console.error(data)
|
||||
|
||||
},
|
||||
|
||||
reloadCss: function(){
|
||||
var queryString = '?reload=' + Date.now()
|
||||
DOM.each(document, 'link[rel="stylesheet"][hot-reload]', (el)=>{
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@ module.exports = {
|
|||
editor_addtab: 'Add tab',
|
||||
editor_delete: 'Delete',
|
||||
|
||||
editor_fragment: 'Fragment',
|
||||
editor_fragment_export: 'Export',
|
||||
editor_fragment_import: 'Import',
|
||||
|
||||
// loadings
|
||||
loading_upload: 'Uploading file...',
|
||||
loading_newclient: 'New client connecting...',
|
||||
|
|
|
|||
|
|
@ -28,6 +28,11 @@ module.exports = {
|
|||
editor_addtab: 'Ajouter onglet',
|
||||
editor_delete: 'Supprimer',
|
||||
|
||||
editor_fragment: 'Fragment',
|
||||
editor_fragment_export: 'Exporter',
|
||||
editor_fragment_import: 'Importer',
|
||||
|
||||
|
||||
// loadings
|
||||
loading_upload: 'Téléversement...',
|
||||
loading_newclient: 'Connexion d\'un nouveau client...',
|
||||
|
|
|
|||
|
|
@ -9,14 +9,19 @@ var ipc = require('../../ipc/'),
|
|||
{saveAs} = require('file-saver'),
|
||||
widgetManager = require('../widgets'),
|
||||
locales = require('../../locales'),
|
||||
Session = require('./session')
|
||||
Session = require('./session'),
|
||||
{deepCopy, deepEqual} = require('../../utils'),
|
||||
EventEmitter = require('../../events/event-emitter')
|
||||
|
||||
|
||||
var SessionManager = class SessionManager {
|
||||
var SessionManager = class SessionManager extends EventEmitter {
|
||||
|
||||
constructor() {
|
||||
|
||||
super()
|
||||
|
||||
this.session = null
|
||||
this.fragments = {}
|
||||
this.lock = false
|
||||
this.lastDir = null
|
||||
this.setSessionPath('')
|
||||
|
|
@ -41,7 +46,7 @@ var SessionManager = class SessionManager {
|
|||
try {
|
||||
|
||||
uiConsole.clear()
|
||||
this.session = new Session(session)
|
||||
this.session = new Session(session, 'session')
|
||||
container.innerHTML = ''
|
||||
parser.reset()
|
||||
parser.parse({
|
||||
|
|
@ -130,7 +135,7 @@ var SessionManager = class SessionManager {
|
|||
|
||||
open(data) {
|
||||
|
||||
this.load(data.session, ()=>{
|
||||
this.load(data.fileContent, ()=>{
|
||||
this.setSessionPath(data.path)
|
||||
ipc.send('sessionOpened', {path: data.path})
|
||||
})
|
||||
|
|
@ -194,6 +199,44 @@ var SessionManager = class SessionManager {
|
|||
|
||||
}
|
||||
|
||||
setFragment(data) {
|
||||
|
||||
var {path, fileContent} = data
|
||||
|
||||
this.fragments[path] = new Session(fileContent, 'fragment')
|
||||
|
||||
this.trigger('fragment-updated', {path: path})
|
||||
|
||||
|
||||
}
|
||||
|
||||
getFragment(path, data) {
|
||||
|
||||
return this.fragments[path]
|
||||
|
||||
}
|
||||
|
||||
saveFragment(data) {
|
||||
|
||||
uiFilebrowser({extension: 'json', save:true, directory: this.lastDir}, (path)=>{
|
||||
this.lastDir = path[0]
|
||||
ipc.send('sessionSave', {
|
||||
session: new Session({
|
||||
content: data,
|
||||
version: PACKAGE.version
|
||||
}, 'fragment'),
|
||||
path: path
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
loadFragment(path) {
|
||||
|
||||
ipc.send('fragmentLoad', {path})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var sessionManager = new SessionManager()
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@ var semver = require('semver'),
|
|||
|
||||
module.exports = class Session {
|
||||
|
||||
constructor(data) {
|
||||
constructor(data, type) {
|
||||
|
||||
if (data === null) {
|
||||
|
||||
data = {
|
||||
session: {type: 'root'}
|
||||
content: {type: 'root'}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ module.exports = class Session {
|
|||
if (semver.lte(version, converter.version)) {
|
||||
|
||||
if (converter.global) data = converter.global(data)
|
||||
if (converter.widget) this.applyConvert(data.session, converter.widget)
|
||||
if (converter.widget) this.applyConvert(data.content, converter.widget)
|
||||
|
||||
if (converter.warning) warning = true
|
||||
|
||||
|
|
@ -36,13 +36,34 @@ module.exports = class Session {
|
|||
|
||||
new UiModal({title: locales('session_oldversion_title'), content: locales('session_oldversion'), icon: 'exclamation-triangle', closable:true})
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (type === 'session' && data.type === 'fragment') {
|
||||
// opening fragment as a session
|
||||
if (data.content.type === 'tab') {
|
||||
data.content = {type: 'root', tabs: [data.content]}
|
||||
} else {
|
||||
data.content = {type: 'root', widgets: [data.content]}
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'fragment' && data.type === 'session') {
|
||||
// opening session as a fragment
|
||||
data.content.type = 'panel'
|
||||
}
|
||||
|
||||
data.createdWith = PACKAGE.productName
|
||||
data.version = PACKAGE.version
|
||||
data.type = PACKAGE.productName + ' ' + 'session'
|
||||
if (type) data.type = type
|
||||
|
||||
// push content to end of file
|
||||
var c = data.content
|
||||
delete data.content
|
||||
data.content = c
|
||||
|
||||
this.data = data
|
||||
|
||||
|
|
@ -68,7 +89,7 @@ module.exports = class Session {
|
|||
|
||||
getRoot() {
|
||||
|
||||
return this.data.session
|
||||
return this.data.content
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -250,6 +271,16 @@ var converters = [
|
|||
data.script = String(data.script).replace('toolbar(4)', 'toolbar(5)').replace('toolbar(3)', 'toolbar(4)')
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
{
|
||||
version: '1.8.15',
|
||||
global: (data)=>{
|
||||
|
||||
data.content = data.session
|
||||
delete data.session
|
||||
return data
|
||||
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
|||
135
src/client/widgets/containers/fragment.js
Normal file
135
src/client/widgets/containers/fragment.js
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
var Container = require('../common/container'),
|
||||
widgetManager = require('../../managers/widgets'),
|
||||
resize = require('../../events/resize'),
|
||||
parser = require('../../parser'),
|
||||
html = require('nanohtml'),
|
||||
sessionManager
|
||||
|
||||
var excludedfragmentClasses = ['widget', 'absolute-position', 'not-editable', 'editing', 'flex-expand', 'no-interaction']
|
||||
|
||||
class Fragment extends Container() {
|
||||
|
||||
static description() {
|
||||
|
||||
return 'Embedded session or fragment with overridable properties.'
|
||||
|
||||
}
|
||||
|
||||
static defaults() {
|
||||
|
||||
var defaults = super.defaults().extend({
|
||||
|
||||
class_specific: {
|
||||
file: {type: 'string', value: '', help: 'fragment file path'},
|
||||
props: {type: 'object', value: {}, help: 'Fragment widget\'s properties to override'},
|
||||
},
|
||||
value: null,
|
||||
osc: null
|
||||
})
|
||||
|
||||
defaults.style = {css: defaults.style.css}
|
||||
|
||||
return defaults
|
||||
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
|
||||
options.props.address = 'auto'
|
||||
options.props.variables = '@{parent.variables}'
|
||||
|
||||
super({...options, html: html`<div class="fragment"></div>`})
|
||||
|
||||
this.noValueState = true
|
||||
|
||||
this.modalBreakout = 0
|
||||
|
||||
this.fragmentClass = []
|
||||
this.container.classList.add('empty')
|
||||
|
||||
this.on('widget-created', (e)=>{
|
||||
|
||||
if (e.widget !== this) {
|
||||
e.widget.container.classList.add('not-editable')
|
||||
e.widget._not_editable = true
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
sessionManager = sessionManager || require('../../managers/session')
|
||||
|
||||
sessionManager.on('fragment-updated', (e)=>{
|
||||
var {path} = e
|
||||
if (path === this.getProp('file')) {
|
||||
this.createFragment()
|
||||
}
|
||||
})
|
||||
|
||||
if (this.getProp('file')) {
|
||||
if (sessionManager.getFragment(this.getProp('file'))) {
|
||||
this.createFragment()
|
||||
} else {
|
||||
sessionManager.loadFragment(this.getProp('file'))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
createFragment() {
|
||||
|
||||
var fragment = sessionManager.getFragment(this.getProp('file'))
|
||||
|
||||
if (!fragment) return
|
||||
|
||||
|
||||
var data = {...deepCopy(fragment.getRoot()), ...this.getProp('props')}
|
||||
|
||||
parser.parse({
|
||||
data: data,
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
|
||||
this.updateContainer()
|
||||
|
||||
}
|
||||
|
||||
|
||||
updateContainer() {
|
||||
|
||||
|
||||
if (this.children[0]) {
|
||||
|
||||
var classes = [...this.children[0].container.classList].filter(i=>excludedfragmentClasses.indexOf(i) === -1)
|
||||
|
||||
if (!deepEqual(classes, this.fragmentClass)) {
|
||||
this.container.classList.remove(...this.fragmentClass)
|
||||
this.fragmentClass = classes
|
||||
this.container.classList.add(...this.fragmentClass)
|
||||
}
|
||||
|
||||
this.container.classList.remove('empty')
|
||||
|
||||
for (var w of this.getAllChildren()) {
|
||||
w.container.classList.add('not-editable')
|
||||
}
|
||||
|
||||
resize.check(this.widget)
|
||||
|
||||
} else if (this.fragmentClass.length) {
|
||||
|
||||
this.container.classList.remove(...this.fragmentClass)
|
||||
this.container.classList.add('empty')
|
||||
this.fragmentClass = []
|
||||
this.widget.innerHTML = ''
|
||||
widgetManager.removeWidgets(this.getAllChildren())
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = Fragment
|
||||
|
|
@ -14,6 +14,7 @@ module.exports.widgets = {
|
|||
tab: require('./containers/tab'),
|
||||
modal: require('./containers/modal'),
|
||||
clone: require('./containers/clone'),
|
||||
fragment: require('./containers/fragment'),
|
||||
matrix: require('./containers/matrix'),
|
||||
keyboard: require('./containers/keyboard'),
|
||||
patchbay: require('./containers/patchbay').PatchBay,
|
||||
|
|
@ -54,7 +55,7 @@ module.exports.widgets = {
|
|||
|
||||
module.exports.categories = {
|
||||
'Basics':['button', 'switch', 'dropdown', 'menu', 'input', 'file'],
|
||||
'Containers':['panel', 'modal', 'clone', 'matrix', 'keyboard', 'patchbay'],
|
||||
'Containers':['panel', 'modal', 'clone', 'fragment', 'matrix', 'keyboard', 'patchbay'],
|
||||
'Frames':['frame', 'svg', 'html', 'image'],
|
||||
'Graphs':['plot','eq','visualizer'],
|
||||
'Indicators':['led', 'text'],
|
||||
|
|
|
|||
|
|
@ -354,7 +354,7 @@
|
|||
}
|
||||
|
||||
|
||||
.clone-container {
|
||||
.clone-container, .fragment-container {
|
||||
|
||||
&.empty {
|
||||
@include pseudo-stroke();
|
||||
|
|
@ -367,7 +367,8 @@
|
|||
content: none
|
||||
}
|
||||
|
||||
> .clone {
|
||||
> .clone,
|
||||
> .fragment {
|
||||
|
||||
--widget-spacing: 0;
|
||||
flex: 1;
|
||||
|
|
|
|||
|
|
@ -115,7 +115,28 @@ module.exports = {
|
|||
module.exports.fileRead(data, clientId, true, (result)=>{
|
||||
|
||||
ipc.clients[clientId].sessionPath = data.path // for resolving local file requests
|
||||
ipc.send('sessionOpen', {path: data.path, session: result}, clientId)
|
||||
ipc.send('sessionOpen', {path: data.path, fileContent: result}, clientId)
|
||||
|
||||
}, (error)=>{
|
||||
|
||||
ipc.send('error', `Could not open session file:\n ${error}`)
|
||||
|
||||
})
|
||||
|
||||
},
|
||||
|
||||
fragmentLoad(data, clientId) {
|
||||
// attempt to read session file
|
||||
|
||||
if (Array.isArray(data.path)) data.path = path.resolve(...data.path)
|
||||
|
||||
module.exports.fileRead(data, clientId, true, (result)=>{
|
||||
|
||||
ipc.send('fragmentLoad', {path: data.path, fileContent: result}, clientId)
|
||||
|
||||
}, (error)=>{
|
||||
|
||||
ipc.send('errorLog', `Could not open fragment file:\n ${error}`)
|
||||
|
||||
})
|
||||
|
||||
|
|
@ -147,7 +168,7 @@ module.exports = {
|
|||
|
||||
},
|
||||
|
||||
fileRead(data, clientId, ok, callback) {
|
||||
fileRead(data, clientId, ok, callback, errorCallback) {
|
||||
// private function
|
||||
|
||||
if (!ok) return
|
||||
|
|
@ -173,7 +194,7 @@ module.exports = {
|
|||
}
|
||||
}
|
||||
|
||||
if (error) ipc.send('error', `Could not open file (${error})`)
|
||||
if (error && errorCallback) errorCallback(error)
|
||||
|
||||
})
|
||||
|
||||
|
|
@ -250,6 +271,10 @@ module.exports = {
|
|||
|
||||
ipc.send('stateLoad', {state: result, path: data.path, send: true}, clientId)
|
||||
|
||||
}, (error)=>{
|
||||
|
||||
ipc.send('error', `Could not open state file:\n ${error}`)
|
||||
|
||||
})
|
||||
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue