Skip to content

Commit 3e6f1ac

Browse files
committed
wip ui
1 parent 102e59a commit 3e6f1ac

16 files changed

+266
-638
lines changed

build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function copy (target) {
1818
await cp('src/preview.js', target)
1919
await cp('src/worker.js', target)
2020
await cp('icons/icon.png', target)
21-
await cp('src/template', target)
21+
await cp('src/settings.json', target)
2222
await cp('src/fonts', target)
2323
await cp('src/lib', target)
2424
await cp('src/pages', target)

src/components/editor.js

+65-47
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,70 @@ class EditorTabs extends Tonic {
9898
this.reRender()
9999
}
100100

101+
rename ({ oldId, newId, label }) {
102+
if (!this.state.tabs.has(oldId)) return
103+
104+
const tab = this.state.tabs.get(oldId)
105+
tab.id = newId
106+
tab.path = newId
107+
tab.label = label
108+
109+
this.state.tabs.delete(oldId)
110+
this.state.tabs.set(newId, tab)
111+
112+
this.reRender()
113+
}
114+
115+
async close (id) {
116+
if (!this.state.tabs.has(id)) return
117+
118+
const tab = this.state.tabs.get(id)
119+
120+
if (tab.unsaved) {
121+
this.selectTab(id)
122+
123+
const coDialogConfirm = document.querySelector('dialog-confirm')
124+
const result = await coDialogConfirm.prompt({
125+
type: 'question',
126+
message: 'This file has changes, what do you want to do?',
127+
buttons: [
128+
{ label: 'Abandon', value: 'abandon' },
129+
{ label: 'Save', value: 'save' }
130+
]
131+
})
132+
133+
if (!result.abandon && !result.save) return
134+
135+
if (result.save) {
136+
await this.props.parent.saveCurrentTab()
137+
}
138+
}
139+
140+
this.remove(id)
141+
142+
// if this tab was selected
143+
if (this.state.selectedTabId === id) {
144+
// check if there are any other tabs
145+
if (this.state.tabs.size > 0) {
146+
const tabs = [...this.state.tabs.values()]
147+
const previousSibling = tabs.findLast(t => t.index < tab.index)
148+
const nextSibling = tabs.find(t => t.index > tab.index)
149+
const sibling = previousSibling || nextSibling
150+
151+
if (sibling) {
152+
sibling.selected = true
153+
this.state.selectedTabId = sibling.id
154+
this.selectTab(sibling.id)
155+
}
156+
} else {
157+
// there are no more tabs. empty the editor
158+
this.props.parent.editor.setValue('')
159+
}
160+
}
161+
162+
this.reRender()
163+
}
164+
101165
get tab () {
102166
return this.state.tabs.get(this.state.selectedTabId)
103167
}
@@ -148,53 +212,7 @@ class EditorTabs extends Tonic {
148212
if (event === 'close') {
149213
const parentTab = el.closest('.tab')
150214
const id = parentTab.dataset.id
151-
if (!this.state.tabs.has(id)) return
152-
153-
const tab = this.state.tabs.get(id)
154-
155-
if (tab.unsaved) {
156-
this.selectTab(id)
157-
158-
const coDialogConfirm = document.querySelector('dialog-confirm')
159-
const result = await coDialogConfirm.prompt({
160-
type: 'question',
161-
message: 'This file has changes, what do you want to do?',
162-
buttons: [
163-
{ label: 'Abandon', value: 'abandon' },
164-
{ label: 'Save', value: 'save' }
165-
]
166-
})
167-
168-
if (!result.abandon && !result.save) return
169-
170-
if (result.save) {
171-
await this.props.parent.saveCurrentTab()
172-
}
173-
}
174-
175-
this.remove(id)
176-
177-
// if this tab was selected
178-
if (this.state.selectedTabId === id) {
179-
// check if there are any other tabs
180-
if (this.state.tabs.size > 0) {
181-
const tabs = [...this.state.tabs.values()]
182-
const previousSibling = tabs.findLast(t => t.index < tab.index)
183-
const nextSibling = tabs.find(t => t.index > tab.index)
184-
const sibling = previousSibling || nextSibling
185-
186-
if (sibling) {
187-
sibling.selected = true
188-
this.state.selectedTabId = sibling.id
189-
this.selectTab(sibling.id)
190-
}
191-
} else {
192-
// there are no more tabs. empty the editor
193-
this.props.parent.editor.setValue('')
194-
}
195-
}
196-
197-
this.reRender()
215+
this.close(id)
198216
}
199217
}
200218

src/components/project.js

+84-10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ class AppProject extends Tonic {
5252
mouseMoveThreshold = 0
5353
timeoutMouseMove = 0
5454

55+
constructor () {
56+
super()
57+
58+
window.addEventListener('keyup', e => {
59+
if (this.mouseIsDragging && e.key === 'Escape') {
60+
this.resetMouse()
61+
this.load()
62+
}
63+
})
64+
}
65+
5566
/**
5667
* auto-sort="false"
5768
*/
@@ -136,6 +147,8 @@ class AppProject extends Tonic {
136147
if (!node) this.getNodeFromElement(el.parentElement)
137148
if (!node) return
138149

150+
if (node.nonMovable) return
151+
139152
this.removeAttribute('dragging')
140153
this.mouseMoveThreshold = 0
141154

@@ -211,6 +224,10 @@ class AppProject extends Tonic {
211224
}
212225

213226
mousemove (e) {
227+
if (Tonic.match(e.target, '[data-event="rename"]')) {
228+
return // renaming, can't drag
229+
}
230+
214231
if (this.mouseIsDown) {
215232
++this.mouseMoveThreshold
216233

@@ -278,13 +295,26 @@ class AppProject extends Tonic {
278295
// Rename a node in the tree
279296
//
280297
if (Tonic.match(e.target, '[data-event="rename"]')) {
298+
if (e.key === 'Escape') {
299+
this.load()
300+
}
301+
281302
if (e.key === 'Enter') {
282303
const value = e.target.value.trim()
283304
if (this.getNodeByProperty('id', value)) return
284305

285-
const newId = path.join(path.dirname(node.id), value)
306+
const dirname = path.dirname(node.id).replace(/%20/g, ' ')
307+
const newId = path.join(dirname, value)
286308
await fs.promises.rename(node.id, newId)
309+
310+
const coTabs = document.querySelector('editor-tabs')
311+
if (coTabs && coTabs.tab.id === node.id) {
312+
coTabs.rename({ oldId: coTabs.tab.id, newId, label: value })
313+
}
314+
287315
node.label = value
316+
node.id = newId
317+
288318
this.load()
289319
}
290320
}
@@ -293,6 +323,10 @@ class AppProject extends Tonic {
293323
async dblclick (e) {
294324
this.resetMouse()
295325

326+
if (Tonic.match(e.target, '[data-event="rename"]')) {
327+
return // already renaming
328+
}
329+
296330
const el = Tonic.match(e.target, '[data-path]')
297331
if (!el) return
298332

@@ -475,6 +509,8 @@ class AppProject extends Tonic {
475509

476510
this.state.currentProject = projectNode.id
477511

512+
if (node.isDirectory) return
513+
478514
const coImagePreview = document.querySelector('view-image-preview')
479515
const coHome = document.querySelector('view-home')
480516

@@ -578,8 +614,9 @@ class AppProject extends Tonic {
578614
}
579615

580616
async load () {
581-
const oldState = this.state.tree
582-
const oldChild = this.getNodeByProperty('id', 'home', oldState)
617+
const app = this.props.parent
618+
let oldState = this.state.tree
619+
let oldChild = this.getNodeByProperty('id', 'home', oldState)
583620

584621
const tree = {
585622
id: 'root',
@@ -594,6 +631,7 @@ class AppProject extends Tonic {
594631
isDirectory: false,
595632
icon: 'home-icon',
596633
label: 'Home',
634+
nonMovable: true,
597635
children: []
598636
})
599637

@@ -619,7 +657,6 @@ class AppProject extends Tonic {
619657
state: oldChild?.state ?? 0,
620658
isDirectory: entry.isDirectory(),
621659
label: entry.name,
622-
data: {},
623660
mime: await lookup(path.extname(entry.name)),
624661
children: []
625662
}
@@ -650,12 +687,49 @@ class AppProject extends Tonic {
650687
}
651688
}
652689

653-
try {
654-
await readDir(path.join(path.DATA, 'projects'), tree)
655-
this.state.tree = tree
656-
} catch (err) {
657-
console.error('Error initiating read directory operation:', err)
658-
return
690+
const settingsId = path.join(path.DATA, 'settings.json')
691+
oldChild = this.getNodeByProperty('id', settingsId, oldState)
692+
693+
tree.children.push({
694+
id: settingsId,
695+
parent: tree,
696+
selected: oldChild?.selected ?? 0,
697+
state: oldChild?.state ?? 0,
698+
isDirectory: false,
699+
icon: 'settings-icon',
700+
label: 'Settings',
701+
nonMovable: true,
702+
children: []
703+
})
704+
705+
const { data: dataProjects } = await app.db.projects.readAll()
706+
707+
for (const [projectId, project] of dataProjects.entries()) {
708+
const oldChild = this.getNodeByProperty('id', project.path, oldState)
709+
710+
const node = {
711+
parent: tree,
712+
selected: oldChild?.selected ?? 0,
713+
state: oldChild?.state ?? 0,
714+
id: project.path,
715+
label: project.label,
716+
isDirectory: true,
717+
nonMovable: true,
718+
icon: 'package',
719+
children: []
720+
}
721+
722+
tree.children.push(node)
723+
724+
if (project.path) {
725+
try {
726+
await readDir(project.path, node)
727+
this.state.tree = tree
728+
} catch (err) {
729+
console.error('Error initiating read directory operation:', err)
730+
return
731+
}
732+
}
659733
}
660734

661735
this.reRender()

src/components/sprite.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class AppSprite extends Tonic {
177177
<line stroke="currentColor" x1="65.3" y1="11.9" x2="81" y2="27.6"/>
178178
</symbol>
179179
180-
<symbol id="settings" viewBox="0 0 100 100">
180+
<symbol id="settings-icon" viewBox="0 0 100 100">
181181
<path fill="currentColor" d="M85.7,52.2c-1.1-0.5-2.1-1.1-3.2-1.7L82,50.1c-1-0.5-1.9-1-2.8-1.5c-0.4-0.2-0.7-0.4-0.8-1.2c-0.1-1.1-0.4-2.6-1-4.1
182182
c-0.3-0.9-0.2-1.3,0.2-1.9c0.8-1,1.5-2.1,2.3-3.2c0.7-0.9,1.3-1.9,2-2.7c1.4-1.9,0.3-3.4-0.1-4c-0.5-0.5-1-1.2-1.5-1.9l-3.7-4.7
183183
c-0.8-1-2.1-1.3-3.3-0.9l-2.2,0.9c-1.6,0.7-3.3,1.4-4.9,2.1c-0.5,0.2-0.9,0.2-1.5-0.1c-1.2-0.7-2.6-1.4-4.1-2

src/components/subscribe.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ export class DialogSubscribe extends TonicDialog {
2626
const coInput = this.querySelector('#subscribe-shared-secret')
2727
const url = new URL(coInput.value.trim())
2828

29-
const cId = url.searchParams.get('clusterId')
30-
const bundleId = url.searchParams.get('bundleId')
29+
const cId = url.searchParams.get('org')
30+
const bundleId = url.searchParams.get('id')
31+
32+
// union://foo?id=com.demo.project&org=test
3133

3234
if (!bundleId || !cId) {
3335
const notifications = document.querySelector('#notifications')
@@ -58,10 +60,30 @@ export class DialogSubscribe extends TonicDialog {
5860

5961
await app.db.projects.put(bundleId, project)
6062
await app.initNetwork()
63+
64+
const coProject = document.querySelector('app-project')
65+
await this.close()
66+
coProject.reRender()
6167
}
6268
}
6369

6470
async render () {
71+
const app = this.props.parent
72+
const { data: dataProjects } = await app.db.projects.readAll()
73+
74+
/* const existingProjects
75+
76+
for (const [projectId, project] of dataProjects.entries()) {
77+
78+
tree.children.push({
79+
id: project.bundleId,
80+
label: project.bundleId,
81+
isDirectory: false,
82+
icon: 'package',
83+
children: []
84+
})
85+
} */
86+
6587
return this.html`
6688
<header>
6789
Create Subscription

src/css/view-image-preview.css

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
view-image-preview {
22
position: absolute;
33
top: 0; bottom: 0; left: 0; right: 0;
4-
overflow: auto;
54
z-index: -1;
65
opacity: 0;
76
transition: z-index .1s ease, opacity .1s ease;
8-
9-
display: grid;
10-
justify-content: center;
11-
align-items: center;
12-
grid-template-rows: 1fr 1fr;
13-
grid-template-columns: 1fr;
147
background-color: var(--tonic-background);
15-
padding: 10%;
168
}
179

1810
view-image-preview.show {
1911
opacity: 1;
2012
z-index: 30;
2113
}
2214

15+
view-image-preview .container {
16+
position: absolute;
17+
top: 50px; bottom: 0; left: 0; right: 0;
18+
display: grid;
19+
justify-content: center;
20+
align-items: center;
21+
grid-template-rows: 1fr 1fr;
22+
grid-template-columns: 1fr;
23+
overflow: auto;
24+
padding: 6%;
25+
}
26+
2327
view-image-preview .top {
2428
display: grid;
2529
justify-content: center;

0 commit comments

Comments
 (0)