@gramlot/gramlot 0.3.8 → 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/editors.js +116527 -0
- package/dist/components/inputs.js +24507 -0
- package/dist/components/layout.js +1402 -0
- package/dist/components/tree.js +24538 -0
- package/dist/components/typst.js +4 -0
- package/dist/gramlot.js +751 -28
- package/dist/gramlot.min.js +27 -24
- package/dist/runtime-notices.json +208 -0
- package/dist/typst-worker-source.js +4 -0
- package/package.json +34 -3
- package/src/binding/hooks.js +14 -0
- package/src/binding/providers.js +4 -0
- package/src/binding/remote.js +2 -0
- package/src/binding/runtime.js +2 -4
- package/src/components/bases.js +1 -0
- package/src/components/buttons.js +3 -0
- package/src/components/choice-editor.js +17 -3
- package/src/components/choices.js +3 -2
- package/src/components/code-mirror-engine.js +91 -0
- package/src/components/code-mirror.js +88 -0
- package/src/components/editor-options.d.ts +9 -0
- package/src/components/editor-options.js +21 -0
- package/src/components/editors.js +10 -0
- package/src/components/html-document.js +56 -0
- package/src/components/html-editor.js +68 -0
- package/src/components/html-preview.js +33 -0
- package/src/components/inputs.js +2 -2
- package/src/components/jodit-engine.js +89 -0
- package/src/components/markdown-codec.js +35 -0
- package/src/components/markdown-document.js +14 -0
- package/src/components/markdown-editor.js +68 -0
- package/src/components/prose-mirror-engine.js +65 -0
- package/src/components/typst-client.js +38 -0
- package/src/components/typst-preview.js +88 -0
- package/src/components/typst-worker.js +49 -0
- package/src/components/typst.js +4 -0
- package/src/filesystem.d.ts +22 -0
- package/src/filesystem.js +122 -0
- package/src/ide-documents.js +153 -0
- package/src/ide-runtime.d.ts +18 -0
- package/src/ide-runtime.js +238 -0
- package/src/index.d.ts +4 -1
- package/src/index.js +4 -1
- package/src/renderer/gramlot-renderer.js +3 -2
- package/src/server/page.d.ts +5 -0
- package/src/server/page.js +4 -0
- package/src/view/button.js +2 -1
- package/src/view/controls.js +7 -11
- package/src/view/events.js +4 -8
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import {Bag} from '@genrojs/bag';
|
|
2
|
+
|
|
3
|
+
/** UTF-8 identity is safe in Bag paths and keeps distinct roots/files distinct. */
|
|
4
|
+
export const documentKey = (root, path) => 'd_' + [...new TextEncoder().encode(JSON.stringify([root,path]))]
|
|
5
|
+
.map(byte => byte.toString(16).padStart(2,'0')).join('');
|
|
6
|
+
|
|
7
|
+
/** Drafts and saved baselines live in Data; only request lifetime lives here. */
|
|
8
|
+
export class IdeDocuments {
|
|
9
|
+
constructor(gramlot, state) {
|
|
10
|
+
this.gramlot = gramlot; this.state = state;
|
|
11
|
+
this.pending = new Map(); this.disposed = false;
|
|
12
|
+
}
|
|
13
|
+
get documents() { return this.state.getItem('documents'); }
|
|
14
|
+
async open(root, path) {
|
|
15
|
+
const key = documentKey(root, path);
|
|
16
|
+
const existing = this.documents.getNodes().find(node => !node.value.getItem('isNew') && node.value.getItem('root') === root && node.value.getItem('path') === path);
|
|
17
|
+
if (existing) return existing.label;
|
|
18
|
+
if (this.pending.has(key)) return this.pending.get(key).promise;
|
|
19
|
+
const request = new AbortController();
|
|
20
|
+
const promise = this.gramlot.rpc.call('data', 'document_read', {root,path}, {signal:request.signal}).then(result => {
|
|
21
|
+
if (this.disposed || request.signal.aborted) return null;
|
|
22
|
+
const document = new Bag();
|
|
23
|
+
for (const [name,value] of Object.entries({...result, root, saved:result.content, busy:false})) document.setItem(name,value);
|
|
24
|
+
this.documents.setItem(key, document);
|
|
25
|
+
return key;
|
|
26
|
+
}).finally(() => this.pending.delete(key));
|
|
27
|
+
this.pending.set(key,{request,promise});
|
|
28
|
+
return promise;
|
|
29
|
+
}
|
|
30
|
+
newDocument(language, extension, root = '') {
|
|
31
|
+
const key = `new_${crypto.randomUUID().replaceAll('-','')}`;
|
|
32
|
+
const doc = new Bag();
|
|
33
|
+
for (const [name,value] of Object.entries({language, root, path:`Untitled${extension}`,
|
|
34
|
+
content:'', saved:'', writable:true, busy:false, isNew:true, createHidden:true,
|
|
35
|
+
saveRoot:root, savePath:`Untitled${extension}`})) doc.setItem(name,value);
|
|
36
|
+
this.documents.setItem(key,doc);
|
|
37
|
+
return key;
|
|
38
|
+
}
|
|
39
|
+
async create(key) {
|
|
40
|
+
const doc = this.documents.getItem(key);
|
|
41
|
+
if (!doc || !doc.getItem('isNew') || this.pending.has(key)) return false;
|
|
42
|
+
const root = doc.getItem('saveRoot'), path = doc.getItem('savePath');
|
|
43
|
+
if (doc.getItem('path').endsWith('.typ.py') !== path.endsWith('.typ.py')) {
|
|
44
|
+
throw new Error('The .typ.py extension is required for the Typst Python recipe document type.');
|
|
45
|
+
}
|
|
46
|
+
const content = doc.getItem('content'), request = new AbortController();
|
|
47
|
+
doc.setItem('busy',true);
|
|
48
|
+
const promise = this.gramlot.rpc.call('data','document_create',{root,path,content},
|
|
49
|
+
{signal:request.signal}).then(result => {
|
|
50
|
+
if (this.disposed || request.signal.aborted) return false;
|
|
51
|
+
doc.setItem('root',result.root);
|
|
52
|
+
doc.setItem('path',result.path);
|
|
53
|
+
doc.setItem('revision',result.revision);
|
|
54
|
+
doc.setItem('saved',content);
|
|
55
|
+
doc.setItem('isNew',false);
|
|
56
|
+
doc.setItem('createHidden',true);
|
|
57
|
+
return true;
|
|
58
|
+
}).finally(() => { this.pending.delete(key); if (!this.disposed) doc.setItem('busy',false); });
|
|
59
|
+
this.pending.set(key,{request,promise});
|
|
60
|
+
return promise;
|
|
61
|
+
}
|
|
62
|
+
invalidateRecipe(key) {
|
|
63
|
+
const doc = this.documents.getItem(key);
|
|
64
|
+
if (!doc.getItem('path').endsWith('.typ.py')) return;
|
|
65
|
+
if (doc.getItem('rendering')) this.pending.get(key)?.request.abort();
|
|
66
|
+
doc.setItem('previewHidden',true);
|
|
67
|
+
doc.setItem('typstSource','');
|
|
68
|
+
doc.setItem('renderStatus','Generate preview to render the current Python recipe.');
|
|
69
|
+
}
|
|
70
|
+
async renderRecipe(key) {
|
|
71
|
+
const doc = this.documents.getItem(key);
|
|
72
|
+
if (!doc || !doc.getItem('path').endsWith('.typ.py') || !doc.getItem('writable') || this.pending.has(key)) return false;
|
|
73
|
+
const content = doc.getItem('content'), request = new AbortController();
|
|
74
|
+
doc.setItem('busy',true);
|
|
75
|
+
doc.setItem('rendering',true);
|
|
76
|
+
doc.setItem('previewHidden',true);
|
|
77
|
+
doc.setItem('typstSource','');
|
|
78
|
+
doc.setItem('renderStatus','Generating Typst on the server…');
|
|
79
|
+
const promise = this.gramlot.rpc.call('data','document_render_typst',{content},
|
|
80
|
+
{signal:request.signal}).then(result => {
|
|
81
|
+
if (this.disposed || request.signal.aborted || doc.getItem('content') !== content) return false;
|
|
82
|
+
doc.setItem('typstSource',result.source);
|
|
83
|
+
doc.setItem('previewHidden',false);
|
|
84
|
+
doc.setItem('renderStatus','Typst generated from the current recipe.');
|
|
85
|
+
return true;
|
|
86
|
+
}).catch(error => {
|
|
87
|
+
if (!this.disposed && !request.signal.aborted) doc.setItem('renderStatus',error.message);
|
|
88
|
+
return false;
|
|
89
|
+
}).finally(() => {
|
|
90
|
+
this.pending.delete(key);
|
|
91
|
+
if (!this.disposed) { doc.setItem('rendering',false); doc.setItem('busy',false); }
|
|
92
|
+
});
|
|
93
|
+
this.pending.set(key,{request,promise});
|
|
94
|
+
return promise;
|
|
95
|
+
}
|
|
96
|
+
dirty(key) {
|
|
97
|
+
const doc = this.documents.getItem(key);
|
|
98
|
+
return doc && doc.getItem('content') !== doc.getItem('saved');
|
|
99
|
+
}
|
|
100
|
+
async save(key) {
|
|
101
|
+
const doc = this.documents.getItem(key);
|
|
102
|
+
if (!doc || doc.getItem('isNew') || !this.dirty(key) || !doc.getItem('writable') || this.pending.has(key)) return false;
|
|
103
|
+
const content = doc.getItem('content'), request = new AbortController();
|
|
104
|
+
doc.setItem('busy',true);
|
|
105
|
+
const promise = this.gramlot.rpc.call('data','document_save', {
|
|
106
|
+
root:doc.getItem('root'), path:doc.getItem('path'), content, revision:doc.getItem('revision')
|
|
107
|
+
}, {signal:request.signal}).then(result => {
|
|
108
|
+
if (this.disposed || request.signal.aborted) return false;
|
|
109
|
+
doc.setItem('revision',result.revision);
|
|
110
|
+
doc.setItem('saved',content);
|
|
111
|
+
return true;
|
|
112
|
+
}).finally(() => { this.pending.delete(key); if (!this.disposed) doc.setItem('busy',false); });
|
|
113
|
+
this.pending.set(key,{request,promise});
|
|
114
|
+
return promise;
|
|
115
|
+
}
|
|
116
|
+
revert(key) {
|
|
117
|
+
const doc = this.documents.getItem(key);
|
|
118
|
+
if (!doc || !this.dirty(key) || this.pending.has(key)) return false;
|
|
119
|
+
doc.setItem('content',doc.getItem('saved'));
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
canClose(key, discard = false) {
|
|
123
|
+
return !this.pending.has(key) && (discard || !this.dirty(key));
|
|
124
|
+
}
|
|
125
|
+
close(key, discard = false) {
|
|
126
|
+
if (!this.canClose(key,discard)) return false;
|
|
127
|
+
this.documents.popNode(key);
|
|
128
|
+
if (this.state.getItem('active') === key) this.state.setItem('active',this.documents.getNodes()[0]?.label ?? null);
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
dispose() { this.disposed = true; for (const {request} of this.pending.values()) request.abort(); }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Preferences only: six successfully opened roots, never document content. */
|
|
135
|
+
export class RecentWorkspaces {
|
|
136
|
+
constructor(storage) { this.storage = storage; this.key = 'gramlot.ide.recentWorkspaces'; }
|
|
137
|
+
read() {
|
|
138
|
+
const stored = this.storage.getItem(this.key);
|
|
139
|
+
const roots = stored === null ? [] : JSON.parse(stored);
|
|
140
|
+
if (!Array.isArray(roots) || roots.some(root => typeof root !== 'string' || !root)) throw new TypeError('Invalid recent workspace preference');
|
|
141
|
+
return roots.slice(0,6);
|
|
142
|
+
}
|
|
143
|
+
remember(root) {
|
|
144
|
+
const roots = [root, ...this.read().filter(item => item !== root)].slice(0,6);
|
|
145
|
+
this.storage.setItem(this.key,JSON.stringify(roots));
|
|
146
|
+
return roots;
|
|
147
|
+
}
|
|
148
|
+
choices(roots = this.read()) {
|
|
149
|
+
const bag = new Bag();
|
|
150
|
+
roots.forEach((root,index) => bag.setItem(`r${index}`,root,{caption:root}));
|
|
151
|
+
return bag;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type {SourceBagNode} from '@genrojs/builders';
|
|
2
|
+
import type {Gramlot} from './gramlot.js';
|
|
3
|
+
/** Export as the page's Logic; constructor-free methods are registered by Gramlot. */
|
|
4
|
+
export class IdeLogic {
|
|
5
|
+
page: Gramlot;
|
|
6
|
+
fileSystemMount(node:SourceBagNode):void;
|
|
7
|
+
fileSystemOpen(node:SourceBagNode,args:{root:string|null}):Promise<string|null>|undefined;
|
|
8
|
+
ideMount(node:SourceBagNode):void;
|
|
9
|
+
ideOpenWorkspace():void;
|
|
10
|
+
ideOpenDocument(root:string,path:string):Promise<void>;
|
|
11
|
+
ideSave(node?:SourceBagNode,args?:{documentKey?:string|null}):Promise<void>;
|
|
12
|
+
ideRevert(node:SourceBagNode,args:{documentKey:string}):void;
|
|
13
|
+
ideUndo(node:SourceBagNode,args:{documentKey:string}):void;
|
|
14
|
+
ideRedo(node:SourceBagNode,args:{documentKey:string}):void;
|
|
15
|
+
ideHistoryChanged(node:SourceBagNode,args:{canUndo:boolean,canRedo:boolean}):void;
|
|
16
|
+
ideClose(node:SourceBagNode):boolean;
|
|
17
|
+
ideClosed(node:SourceBagNode,args:{pageName:string}):void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import {sourceTarget} from '@genrojs/builders';
|
|
2
|
+
import {FileSystemLogic} from './filesystem.js';
|
|
3
|
+
import {IdeDocuments, RecentWorkspaces} from './ide-documents.js';
|
|
4
|
+
import {codeMirrorWithOptions} from './components/editor-options.js';
|
|
5
|
+
|
|
6
|
+
/** Reusable IDE coordination uses Source for panels and Data for all document state. */
|
|
7
|
+
export class IdeLogic {
|
|
8
|
+
fileSystemMount(node) { return FileSystemLogic.prototype.fileSystemMount.call(this,node); }
|
|
9
|
+
fileSystemOpen(node, args) { return FileSystemLogic.prototype.fileSystemOpen.call(this,node,args); }
|
|
10
|
+
ideMount(node) {
|
|
11
|
+
this.ideRoot = node.parentNode;
|
|
12
|
+
this.ideState = node.getRelativeData('.');
|
|
13
|
+
this.ideDocuments = new IdeDocuments(this.page,this.ideState);
|
|
14
|
+
this.idePanels = new Map();
|
|
15
|
+
this.ideDisposed = false;
|
|
16
|
+
this.ideRequested = 0;
|
|
17
|
+
this.ideRecent = new RecentWorkspaces(this.page.src.renderer.destination.ownerDocument.defaultView.sessionStorage);
|
|
18
|
+
const roots = this.ideRecent.read();
|
|
19
|
+
this.ideState.setItem('recent',this.ideRecent.choices(roots));
|
|
20
|
+
this.ideState.setItem('workspace',roots[0] ?? '');
|
|
21
|
+
const browser = this.ideState.getItem('browser');
|
|
22
|
+
browser.subscribe('ide-browser',{any:event => {
|
|
23
|
+
if (event.node === browser.getNode('root')) {
|
|
24
|
+
const root = browser.getItem('root');
|
|
25
|
+
this.ideState.setItem('workspace',root);
|
|
26
|
+
this.ideState.setItem('recent',this.ideRecent.choices(this.ideRecent.remember(root)));
|
|
27
|
+
this.ideState.setItem('status',`Workspace: ${root}`);
|
|
28
|
+
} else if (event.node === browser.getNode('selectedPath')) {
|
|
29
|
+
const path = browser.getItem('selectedPath');
|
|
30
|
+
const item = path === null ? null : browser.getItem('store').getNode(path);
|
|
31
|
+
if (item && !item.attr.is_directory) void this.ideOpenDocument(browser.getItem('root'),item.attr.path);
|
|
32
|
+
}
|
|
33
|
+
}});
|
|
34
|
+
this.ideDocuments.documents.subscribe('ide-documents',{any:event => {
|
|
35
|
+
if (!['content','saved','busy','writable','view','isNew','path','root'].includes(event.node.label)) return;
|
|
36
|
+
for (const [key,pane] of this.idePanels) {
|
|
37
|
+
const doc = this.ideDocuments.documents.getItem(key);
|
|
38
|
+
if (doc) {
|
|
39
|
+
if (event.node === doc.getNode('content')) this.ideDocuments.invalidateRecipe(key);
|
|
40
|
+
this.ideDocumentState(key);
|
|
41
|
+
pane.setAttr({title:`${this.ideDocuments.dirty(key) ? '● ' : ''}${doc.getItem('path')}`});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}});
|
|
45
|
+
this.page.src.renderer.onDispose(this.ideRoot,() => {
|
|
46
|
+
this.ideDisposed = true;
|
|
47
|
+
browser.unsubscribe('ide-browser',{any:true});
|
|
48
|
+
this.ideDocuments.documents.unsubscribe('ide-documents',{any:true});
|
|
49
|
+
this.ideDocuments.dispose();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
ideOpenWorkspace() {
|
|
53
|
+
const root = this.ideState.getItem('workspace');
|
|
54
|
+
if (root && root !== this.ideState.getItem('browser.root')) this.ideState.setItem('browser.requestedRoot',root);
|
|
55
|
+
}
|
|
56
|
+
ideDocumentState(key) {
|
|
57
|
+
const doc = this.ideDocuments.documents.getItem(key);
|
|
58
|
+
const disabled = (!this.ideDocuments.dirty(key) && !doc.getItem('isNew')) || doc.getItem('busy');
|
|
59
|
+
doc.setItem('saveDisabled',Boolean(disabled || !doc.getItem('writable')));
|
|
60
|
+
doc.setItem('revertDisabled',Boolean(!this.ideDocuments.dirty(key) || doc.getItem('busy')));
|
|
61
|
+
doc.setItem('displayPath',doc.getItem('isNew') ? doc.getItem('path') : `${doc.getItem('root')}/${doc.getItem('path')}`);
|
|
62
|
+
doc.setItem('pdfFilename',doc.getItem('path').split('/').at(-1).replace(/\.typ(?:\.py)?$/i,'.pdf'));
|
|
63
|
+
doc.setItem('renderDisabled',Boolean(doc.getItem('busy') || !doc.getItem('writable')));
|
|
64
|
+
this.ideHistoryState(doc);
|
|
65
|
+
}
|
|
66
|
+
async ideOpenDocument(root, path) {
|
|
67
|
+
const requested = ++this.ideRequested;
|
|
68
|
+
try {
|
|
69
|
+
const key = await this.ideDocuments.open(root,path);
|
|
70
|
+
if (key === null || this.ideDisposed) return;
|
|
71
|
+
this.ideShowDocument(key);
|
|
72
|
+
if (requested === this.ideRequested) {
|
|
73
|
+
this.ideState.setItem('active',key);
|
|
74
|
+
this.ideState.setItem('status',`${path}${this.ideDocuments.documents.getItem(key).getItem('writable') ? '' : ' (read only)'}`);
|
|
75
|
+
}
|
|
76
|
+
} catch (error) { if (!this.ideDisposed) this.ideState.setItem('status',error.message); }
|
|
77
|
+
}
|
|
78
|
+
ideNew() {
|
|
79
|
+
const type = this.ideState.getItem('newType');
|
|
80
|
+
const language = type === 'typstRecipe' ? 'python' : type;
|
|
81
|
+
const extensions = {typstRecipe:'.typ.py',typst:'.typ',markdown:'.md',html:'.html',python:'.py',javascript:'.js',
|
|
82
|
+
typescript:'.ts',json:'.json',css:'.css',sql:'.sql',xml:'.xml',text:'.txt'};
|
|
83
|
+
const key = this.ideDocuments.newDocument(language,extensions[type],this.ideState.getItem('workspace'));
|
|
84
|
+
++this.ideRequested;
|
|
85
|
+
this.ideShowDocument(key);
|
|
86
|
+
this.ideState.setItem('active',key);
|
|
87
|
+
this.ideState.setItem('status','New document. Choose directory and name when saving.');
|
|
88
|
+
}
|
|
89
|
+
ideShowDocument(key) {
|
|
90
|
+
const document = this.ideDocuments.documents.getItem(key);
|
|
91
|
+
const path = document.getItem('path');
|
|
92
|
+
if (!this.idePanels.has(key)) {
|
|
93
|
+
const doc = this.ideDocuments.documents.getItem(key);
|
|
94
|
+
const recipe = path.endsWith('.typ.py');
|
|
95
|
+
if (recipe) this.ideDocuments.invalidateRecipe(key);
|
|
96
|
+
doc.setItem('readonly',!doc.getItem('writable'));
|
|
97
|
+
this.ideDocumentState(key);
|
|
98
|
+
doc.setItem('view','code');
|
|
99
|
+
doc.setItem('createHidden',true);
|
|
100
|
+
doc.setItem('undoDisabled',true);
|
|
101
|
+
doc.setItem('redoDisabled',true);
|
|
102
|
+
const tabs = this.ideRoot.value.getNode('frame.center.tabs');
|
|
103
|
+
const pane = sourceTarget(tabs.builder.wrapSource(tabs).contentPane({node_label:key,
|
|
104
|
+
title:path, pageName:key, documentKey:key, closable:true, onClose:'ideClose',
|
|
105
|
+
height:'100%'}));
|
|
106
|
+
this.idePanels.set(key,pane);
|
|
107
|
+
const target = pane.builder.wrapSource(pane).div({display:'flex',flex_direction:'column',height:'100%'});
|
|
108
|
+
const toolbar = target.div({datapath:`.documents.${key}`,display:'flex',align_items:'center',gap:'6px',padding:'4px 12px',
|
|
109
|
+
height:'40px',flex_shrink:0,box_sizing:'border-box',class:'gramlot-ide-document-toolbar',role:'toolbar','aria-label':`Commands for ${path}`});
|
|
110
|
+
for (const [name,path,label,func] of [
|
|
111
|
+
['save','M5 3h12l4 4v14H3V3h2 M7 3v6h10V3 M7 21v-8h10v8','Save','ideSave'],
|
|
112
|
+
['revert','M3 10a9 9 0 1 1 2 8 M3 4v6h6 M12 7v5l3 2','Revert to saved','ideRevert'],
|
|
113
|
+
['undo','M9 5 3 11l6 6 M3 11h12a6 6 0 0 1 6 6v2','Undo','ideUndo'],
|
|
114
|
+
['redo','m15 5 6 6-6 6 M21 11H9a6 6 0 0 0-6 6v2','Redo','ideRedo']]) {
|
|
115
|
+
const button = toolbar.button({title:label,'aria-label':label,disabled:`^.${name}Disabled`,
|
|
116
|
+
class:`button--quiet gramlot-ide-${name}`});
|
|
117
|
+
button.svg({viewBox:'0 0 24 24',width:18,height:18,fill:'none',stroke:'currentColor',
|
|
118
|
+
stroke_width:1.7,stroke_linecap:'round',stroke_linejoin:'round','aria-hidden':'true'}).path({d:path});
|
|
119
|
+
button.dataController({func,documentKey:key});
|
|
120
|
+
}
|
|
121
|
+
if (recipe) toolbar.button('Generate preview',{disabled:'^.renderDisabled',class:'button--quiet'})
|
|
122
|
+
.dataController({func:'ideRenderRecipe',documentKey:key});
|
|
123
|
+
toolbar.div('^.displayPath',{class:'gramlot-ide-document-path',overflow:'hidden',
|
|
124
|
+
text_overflow:'ellipsis',white_space:'nowrap',min_width:0,flex:'1'});
|
|
125
|
+
const form = target.div({datapath:`.documents.${key}`,hidden:'^.createHidden',
|
|
126
|
+
padding:'8px 12px',display:'flex',gap:'8px',align_items:'end',flex_shrink:0});
|
|
127
|
+
form.textbox({value:'^.saveRoot',lbl:'Directory',flex:'1',disabled:'^.busy'});
|
|
128
|
+
form.textbox({value:'^.savePath',lbl:'File name',flex:'1',disabled:'^.busy'});
|
|
129
|
+
form.button('Create file',{disabled:'^.busy'}).dataController({func:'ideCreate',documentKey:key});
|
|
130
|
+
form.button('Cancel',{disabled:'^.busy',class:'button--quiet'}).dataController({func:'ideCancelCreate',documentKey:key});
|
|
131
|
+
const editorArea = target.div({flex:'1',min_height:0});
|
|
132
|
+
const html = doc.getItem('language') === 'html';
|
|
133
|
+
const markdown = doc.getItem('language') === 'markdown';
|
|
134
|
+
const rich = html || markdown;
|
|
135
|
+
const typst = doc.getItem('language') === 'typst';
|
|
136
|
+
const preview = rich || typst || recipe;
|
|
137
|
+
const views = preview ? editorArea.tabContainer({height:'100%',selectedPage:`^.documents.${key}.view`}) : null;
|
|
138
|
+
const code = preview ? views.contentPane({title:'Code',pageName:'code',height:'100%'}) : editorArea;
|
|
139
|
+
codeMirrorWithOptions(code,{datapath:`.documents.${key}`,value:'^.content',language:doc.getItem('language'),
|
|
140
|
+
height:'100%', readonly:'^.readonly',nodeId:`editor_${key}`,
|
|
141
|
+
editorView:'code',selfsubscribe_historyChanged:'ideHistoryChanged'});
|
|
142
|
+
target.dataController({func:'ideSave',documentKey:key,[`subscribe_editor_${key}_save`]:true});
|
|
143
|
+
if (typst) views.contentPane({title:'Preview',pageName:'preview',height:'100%'}).typstPreview({value:`^.documents.${key}.content`,filename:`^.documents.${key}.pdfFilename`,height:'100%'});
|
|
144
|
+
if (recipe) {
|
|
145
|
+
const output = views.contentPane({title:'Preview',pageName:'preview',height:'100%',display:'flex',flex_direction:'column'});
|
|
146
|
+
output.div(`^.documents.${key}.renderStatus`,{role:'status',padding:'8px 12px',white_space:'pre-wrap',overflow:'auto',max_height:'30%'});
|
|
147
|
+
output.typstPreview({value:`^.documents.${key}.typstSource`,filename:`^.documents.${key}.pdfFilename`,
|
|
148
|
+
hidden:`^.documents.${key}.previewHidden`,flex:'1',min_height:0});
|
|
149
|
+
views.contentPane({title:'Generated Typst',pageName:'generated',height:'100%'})
|
|
150
|
+
.codeMirror({value:`^.documents.${key}.typstSource`,language:'typst',readonly:true,height:'100%'});
|
|
151
|
+
}
|
|
152
|
+
if (rich) {
|
|
153
|
+
const previewPane = views.contentPane({title:'Preview',pageName:'preview',height:'100%',display:'flex',flex_direction:'column'});
|
|
154
|
+
if (markdown) {
|
|
155
|
+
doc.setItem('allowHtml',false);
|
|
156
|
+
previewPane.checkbox({value:`^.documents.${key}.allowHtml`,label:'Allow embedded HTML',class:'gramlot-ide-allow-html',padding:'8px 12px',flex_shrink:0});
|
|
157
|
+
}
|
|
158
|
+
previewPane[html ? 'htmlPreview' : 'markdownPreview']({value:`^.documents.${key}.content`,flex:'1',min_height:0,
|
|
159
|
+
...(markdown ? {allowHtml:`^.documents.${key}.allowHtml`} : {})});
|
|
160
|
+
views.contentPane({title:'Rich text',pageName:'rich',height:'100%'})[html ? 'htmlEditor' : 'markdownEditor']({
|
|
161
|
+
datapath:`.documents.${key}`,value:'^.content',readonly:'^.readonly',height:'100%',
|
|
162
|
+
nodeId:`rich_editor_${key}`,editorView:'rich',selfsubscribe_historyChanged:'ideHistoryChanged'});
|
|
163
|
+
target.dataController({func:'ideSave',documentKey:key,[`subscribe_rich_editor_${key}_save`]:true});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async ideRenderRecipe(node, {documentKey}) {
|
|
168
|
+
const doc = this.ideDocuments.documents.getItem(documentKey);
|
|
169
|
+
doc.setItem('view','preview');
|
|
170
|
+
await this.ideDocuments.renderRecipe(documentKey);
|
|
171
|
+
}
|
|
172
|
+
ideCancelCreate(node, {documentKey}) {
|
|
173
|
+
this.ideDocuments.documents.getItem(documentKey).setItem('createHidden',true);
|
|
174
|
+
}
|
|
175
|
+
async ideCreate(node, {documentKey}) {
|
|
176
|
+
this.ideState.setItem('status','Creating file…');
|
|
177
|
+
try {
|
|
178
|
+
const saved = await this.ideDocuments.create(documentKey);
|
|
179
|
+
if (!this.ideDisposed && saved) {
|
|
180
|
+
this.ideState.setItem('status','File created.');
|
|
181
|
+
const browser = this.ideState.getItem('browser');
|
|
182
|
+
if (browser.getItem('root')) await this.fileSystems.get(browser).open(browser.getItem('root'));
|
|
183
|
+
}
|
|
184
|
+
} catch (error) { if (!this.ideDisposed) this.ideState.setItem('status',error.message); }
|
|
185
|
+
}
|
|
186
|
+
async ideSave(node, {documentKey = null} = {}) {
|
|
187
|
+
const key = documentKey ?? this.ideState.getItem('active');
|
|
188
|
+
if (!key || this.ideDocuments.documents.getItem(key).getItem('saveDisabled')) return;
|
|
189
|
+
const doc = this.ideDocuments.documents.getItem(key);
|
|
190
|
+
if (doc.getItem('isNew')) { doc.setItem('createHidden',false); return; }
|
|
191
|
+
this.ideState.setItem('status','Saving…');
|
|
192
|
+
try {
|
|
193
|
+
const saved = await this.ideDocuments.save(key);
|
|
194
|
+
if (!this.ideDisposed) this.ideState.setItem('status', saved ? 'Saved.' : 'Document is read only or a request is pending.');
|
|
195
|
+
} catch (error) { if (!this.ideDisposed) this.ideState.setItem('status',error.message); }
|
|
196
|
+
}
|
|
197
|
+
ideRevert(node, {documentKey}) {
|
|
198
|
+
if (this.ideDocuments.revert(documentKey)) this.ideState.setItem('status','Restored the last saved version.');
|
|
199
|
+
}
|
|
200
|
+
ideHistoryChanged(node, {canUndo,canRedo}) {
|
|
201
|
+
const doc = node.getRelativeData('.');
|
|
202
|
+
const view = node.getAttr('editorView');
|
|
203
|
+
doc.setItem(`${view}CanUndo`,canUndo);
|
|
204
|
+
doc.setItem(`${view}CanRedo`,canRedo);
|
|
205
|
+
this.ideHistoryState(doc);
|
|
206
|
+
}
|
|
207
|
+
ideHistoryState(doc) {
|
|
208
|
+
const view = doc.getItem('view');
|
|
209
|
+
doc.setItem('undoDisabled',!doc.getItem(`${view}CanUndo`));
|
|
210
|
+
doc.setItem('redoDisabled',!doc.getItem(`${view}CanRedo`));
|
|
211
|
+
}
|
|
212
|
+
ideEditor(documentKey) {
|
|
213
|
+
const rich = this.ideDocuments.documents.getItem(documentKey).getItem('view') === 'rich';
|
|
214
|
+
const node = this.page.src.binding.nodeIds.get(`${rich ? 'rich_' : ''}editor_${documentKey}`);
|
|
215
|
+
return this.page.src.renderer.records.get(node).element;
|
|
216
|
+
}
|
|
217
|
+
ideUndo(node, {documentKey}) { this.ideEditor(documentKey).undo(); }
|
|
218
|
+
ideRedo(node, {documentKey}) { this.ideEditor(documentKey).redo(); }
|
|
219
|
+
ideClose(node) {
|
|
220
|
+
const key = node.getAttr('documentKey');
|
|
221
|
+
if (!this.ideDocuments.canClose(key)) {
|
|
222
|
+
this.ideState.setItem('status','Save or revert your changes before closing. Pending operations must finish first.');
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
ideClosed(node, {pageName}) { this.ideForgetDocument(pageName); }
|
|
228
|
+
ideForgetDocument(key, discard = false) {
|
|
229
|
+
const document = this.ideDocuments.documents.getItem(key);
|
|
230
|
+
const browser = this.ideState.getItem('browser'), selectedPath = browser.getItem('selectedPath');
|
|
231
|
+
const selected = selectedPath === null ? null : browser.getItem('store').getNode(selectedPath);
|
|
232
|
+
if (document && browser.getItem('root') === document.getItem('root') && selected?.attr.path === document.getItem('path')) {
|
|
233
|
+
browser.setItem('selectedPath',null);
|
|
234
|
+
}
|
|
235
|
+
this.idePanels.delete(key);
|
|
236
|
+
this.ideDocuments.close(key,discard);
|
|
237
|
+
}
|
|
238
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export {UtilitiesHandler} from './handlers/utilities-handler.js';
|
|
|
14
14
|
export type {GramlotOptions, GramlotState, Publication, SubscriptionHandle, FormatOptions} from './gramlot.js';
|
|
15
15
|
export {PageBootstrap} from './bootstrap.js';
|
|
16
16
|
export type {PageBootstrapOptions, PageResources} from './bootstrap.js';
|
|
17
|
-
export {Exposed, Page} from './server/page.js';
|
|
17
|
+
export {Exposed, Page, codeMirrorWithOptions} from './server/page.js';
|
|
18
18
|
export {GramlotBuilder} from './builder/gramlot-builder.js';
|
|
19
19
|
export {GramlotRenderer} from './renderer/gramlot-renderer.js';
|
|
20
20
|
export {GramlotHtmlRenderer} from './renderer/gramlot-html-renderer.js';
|
|
@@ -39,3 +39,6 @@ export type {DateExpressionOptions, DateExpressionResult, DateExpressionErrorCod
|
|
|
39
39
|
|
|
40
40
|
export {FramePage} from "./server/frame-page.js";
|
|
41
41
|
export {NavigationResolver} from "./navigation-resolver.js";
|
|
42
|
+
|
|
43
|
+
export {fileSystemTree, FileSystemLogic, FileSystemController, DirectoryResolver} from "./filesystem.js";
|
|
44
|
+
export {IdeLogic} from "./ide-runtime.js";
|
package/src/index.js
CHANGED
|
@@ -14,7 +14,7 @@ export {DomHandler} from './handlers/dom-handler.js';
|
|
|
14
14
|
export {UtilitiesHandler} from './handlers/utilities-handler.js';
|
|
15
15
|
export {PageBootstrap} from './bootstrap.js';
|
|
16
16
|
// A page module served for its Logic imports @gramlot/gramlot/page; the bootstrap import map points it here.
|
|
17
|
-
export {Exposed, Page, FramePage} from './server/page.js';
|
|
17
|
+
export {Exposed, Page, FramePage, codeMirrorWithOptions} from './server/page.js';
|
|
18
18
|
export {GramlotBuilder} from './builder/gramlot-builder.js';
|
|
19
19
|
export {GramlotRenderer} from './renderer/gramlot-renderer.js';
|
|
20
20
|
export {GramlotHtmlRenderer} from './renderer/gramlot-html-renderer.js';
|
|
@@ -34,3 +34,6 @@ export {IndexLogic, mountIndexPage} from './index-page-runtime.js';
|
|
|
34
34
|
export {NavigationResolver} from './navigation-resolver.js';
|
|
35
35
|
|
|
36
36
|
export {parseDateExpression} from './date-parser/index.js';
|
|
37
|
+
|
|
38
|
+
export {fileSystemTree, FileSystemLogic, FileSystemController, DirectoryResolver} from "./filesystem.js";
|
|
39
|
+
export {IdeLogic} from "./ide-runtime.js";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {callLogic, invokeHook} from '../binding/hooks.js';
|
|
1
2
|
/* @ts-self-types="./gramlot-renderer.d.ts" */
|
|
2
3
|
/**
|
|
3
4
|
* The live renderer of a Gramlot page.
|
|
@@ -50,8 +51,8 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
|
|
|
50
51
|
const {group, method} = binding.gramlot.src.logicRegistry.resolve(name, node);
|
|
51
52
|
return method.call(group, node, keywords);
|
|
52
53
|
},
|
|
53
|
-
invokeScript: (node, name, code, keywords) =>
|
|
54
|
-
node
|
|
54
|
+
invokeScript: (node, name, code, keywords) => invokeHook(binding, node, name, code, keywords,
|
|
55
|
+
node.parentNode._getMeta('gramlot_component').childParameters[name].inline)});
|
|
55
56
|
this.records = new Map();
|
|
56
57
|
this.references = new References(source);
|
|
57
58
|
this.pending = [];
|
package/src/server/page.d.ts
CHANGED
|
@@ -77,3 +77,8 @@ export {ServerAdapter} from "../server-adapter.js";
|
|
|
77
77
|
* made with `createDecimal('12.50')`. A date or a time is a `Date`; TYTX types it from its content. */
|
|
78
78
|
export {Bag, BagNode} from "@genrojs/bag";
|
|
79
79
|
export {createDecimal} from "@genrojs/tytx";
|
|
80
|
+
|
|
81
|
+
export {codeMirrorWithOptions} from '../components/editor-options.js';
|
|
82
|
+
|
|
83
|
+
export {fileSystemTree, FileSystemLogic, FileSystemController, DirectoryResolver} from "../filesystem.js";
|
|
84
|
+
export {IdeLogic} from "../ide-runtime.js";
|
package/src/server/page.js
CHANGED
|
@@ -9,3 +9,7 @@ export {Bag, BagNode} from '@genrojs/bag';
|
|
|
9
9
|
export {createDecimal} from '@genrojs/tytx';
|
|
10
10
|
// A ServerAdapter module chosen by a server imports its base class from @gramlot/gramlot/page.
|
|
11
11
|
export {ServerAdapter} from '../server-adapter.js';
|
|
12
|
+
export {codeMirrorWithOptions} from '../components/editor-options.js';
|
|
13
|
+
|
|
14
|
+
export {fileSystemTree, FileSystemLogic, FileSystemController, DirectoryResolver} from "../filesystem.js";
|
|
15
|
+
export {IdeLogic} from "../ide-runtime.js";
|
package/src/view/button.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* target selection; this binding owns execution, ask, delay and disposal.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import {invokeHook} from '../binding/hooks.js';
|
|
6
7
|
|
|
7
8
|
export class ButtonBinding {
|
|
8
9
|
#binding;
|
|
@@ -150,7 +151,7 @@ export class ButtonBinding {
|
|
|
150
151
|
if (this.binding.closed || this.element.disabled || result === null) return;
|
|
151
152
|
const values = {...read(), ...result};
|
|
152
153
|
if (result) values._askResult = result;
|
|
153
|
-
this.binding.runtime
|
|
154
|
+
invokeHook(this.binding.runtime, node, 'action', node.getAttr('action'), values);
|
|
154
155
|
};
|
|
155
156
|
const ask = node.getAttr('ask');
|
|
156
157
|
const askDialog = this.binding.runtime.askDialog;
|
package/src/view/controls.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import {createDecimal, isDecimal, getDecimalLibrary} from '@genrojs/tytx';
|
|
9
9
|
import {Bag} from '@genrojs/bag';
|
|
10
10
|
import {XHTML_NS} from '../renderer/attributes.js';
|
|
11
|
+
import {callLogic, invokeHook} from '../binding/hooks.js';
|
|
11
12
|
|
|
12
13
|
// The elements whose `type`, `multiple` and `group` have no setter: a change rebuilds (C04.1, D7).
|
|
13
14
|
const CONTROL_TAGS = new Set(['input', 'select', 'textarea']);
|
|
@@ -214,8 +215,8 @@ class ComponentControl extends ControlAdapter {
|
|
|
214
215
|
}
|
|
215
216
|
get writeEvent() {
|
|
216
217
|
if (this.element.commitOnChange) return 'change';
|
|
217
|
-
const
|
|
218
|
-
return
|
|
218
|
+
const live = this.resolved.live ?? this.element.parameters.live;
|
|
219
|
+
return live && this.element.liveWrites ? 'input' : 'change';
|
|
219
220
|
}
|
|
220
221
|
read() { return this.element.readControlValue(); }
|
|
221
222
|
write(value) { this.element.writeControlValue(value); }
|
|
@@ -569,20 +570,15 @@ const INPUT_CONTROLS = {
|
|
|
569
570
|
function componentContext(adapter) {
|
|
570
571
|
const {binding, node} = adapter, app = binding.runtime.gramlot;
|
|
571
572
|
const invoke = (attribute, code, keywords, expression = false) => {
|
|
572
|
-
if (!expression
|
|
573
|
-
const {group, method} = app.src.logicRegistry.resolve(code, node);
|
|
574
|
-
return method.call(group, node, keywords);
|
|
575
|
-
}
|
|
573
|
+
if (!expression) return invokeHook(binding.runtime, node, attribute, code, keywords);
|
|
576
574
|
return binding.runtime.inlineCompiler.compile(node, attribute, code, Object.keys(keywords),
|
|
577
|
-
|
|
575
|
+
`return (${code});`)(keywords);
|
|
578
576
|
};
|
|
579
577
|
return {
|
|
580
578
|
locale:app.locale, timeZone:app.timeZone, createDecimal, isDecimal, getDecimalLibrary,
|
|
579
|
+
publish:(topic, payload) => node.publish(topic, payload),
|
|
581
580
|
isActive:() => !binding.closed,
|
|
582
|
-
callLogic:(name, keywords) =>
|
|
583
|
-
const {group, method} = app.src.logicRegistry.resolve(name, node);
|
|
584
|
-
return method.call(group, node, keywords);
|
|
585
|
-
},
|
|
581
|
+
callLogic:(name, keywords) => callLogic(binding.runtime, node, name, keywords),
|
|
586
582
|
format:(value, options) => app.format(value, options), invoke,
|
|
587
583
|
remote:(method, keywords, signal) => app.rpc.call('data', method, keywords, {signal}),
|
|
588
584
|
validationCurrent:() => {
|
package/src/view/events.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
+
import {isLogicMethod, callLogic} from '../binding/hooks.js';
|
|
1
2
|
/**
|
|
2
3
|
* `connect_on<event>` (source plan §2 Button, §4.11, S12): one DOM listener per attribute, bound to the
|
|
3
4
|
* element of the node that declares it. The DOM event is the name after `connect_on`, lower-cased
|
|
4
|
-
* (`connect_onclick` → `click`, `connect_onBlur` → `blur`). A
|
|
5
|
-
* (`group.method`) is named logic, resolved by LogicRegistry at every event (a missing name raises,
|
|
5
|
+
* (`connect_onclick` → `click`, `connect_onBlur` → `blur`). A simple or qualified identifier
|
|
6
|
+
* (`method` or `group.method`) is named logic, resolved by LogicRegistry at every event (a missing name raises,
|
|
6
7
|
* no fallback to inline, S09); any other value is inline code with `this` = the node and the
|
|
7
8
|
* parameter `event`. The handler runs with the declaring node, also when the event hits a
|
|
8
9
|
* descendant (R11). A closed NodeBinding does nothing (R12); an error propagates from the listener.
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
12
|
const PREFIX = 'connect_on';
|
|
12
|
-
const NAMED_LOGIC = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/;
|
|
13
13
|
|
|
14
14
|
export class NativeEventBinding {
|
|
15
15
|
#binding;
|
|
@@ -54,11 +54,7 @@ export class NativeEventBinding {
|
|
|
54
54
|
if (this.binding.closed) return;
|
|
55
55
|
const node = this.node;
|
|
56
56
|
const runtime = this.binding.runtime;
|
|
57
|
-
if (
|
|
58
|
-
const {group, method} = runtime.logicRegistry.resolve(this.handler, node);
|
|
59
|
-
method.call(group, node, event);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
57
|
+
if (isLogicMethod(this.handler)) return callLogic(runtime, node, this.handler, event);
|
|
62
58
|
runtime.inlineCompiler.compile(node, this.attribute, this.handler, ['event'])({event});
|
|
63
59
|
}
|
|
64
60
|
}
|