-
Notifications
You must be signed in to change notification settings - Fork 6
/
Perspective.svelte
611 lines (537 loc) · 19.4 KB
/
Perspective.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<script lang="ts">
import type Perspective from '../ad4m/Perspective'
export let perspective: Perspective
import IconButton from '@smui/icon-button';
import Fab, {Icon, Label} from '@smui/fab';
import Link, { hashLinkExpression, linkEqual } from '../ad4m/Links';
import { exprRef2String } from '../ad4m/ExpressionRef';
import ExpressionIcon from './ExpressionIcon.svelte';
import ExpressionBrowser from './ExpressionBrowser.svelte';
import iconComponentFromString from './iconComponentFromString';
import ConstructionMenu from './ConstructionMenu.svelte'
import PerspectiveSettings from './PerspectiveSettings.svelte';
import ExpressionContextMenu from "./ExpressionContextMenu.svelte";
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
import { query, mutation, getClient } from "svelte-apollo";
import { gql } from '@apollo/client';
import { LANGUAGES } from './graphql_queries'
import { linkTo2D, coordToPredicate } from './uiUtils';
const gqlClient = getClient()
$: if(perspective) {
gqlClient.subscribe({
query: gql`
subscription {
linkAdded(perspectiveUUID: "${perspective.uuid}") {
timestamp
}
}
`}).subscribe({
next: () => linksStore.fetchMore({}),
error: (e) => console.error(e)
})
gqlClient.subscribe({
query: gql`
subscription {
linkRemoved(perspectiveUUID: "${perspective.uuid}") {
timestamp
}
}
`}).subscribe({
next: () => linksStore.refetch({}),
error: (e) => console.error(e)
})
}
$: ALL_LINKS_QUERY = gql`{
links(perspectiveUUID: "${perspective.uuid}", query: { }) {
author { did }
timestamp
data {
source
predicate
target
}
}
}`
$: ADD_LINK = mutation(gql`
mutation AddLink($link: String){
addLink(input: { perspectiveUUID: "${perspective.uuid}", link: $link }) {
author { did }
timestamp
data {
source
predicate
target
}
}
}
`)
$: UPDATE_LINK = mutation(gql`
mutation UpdateLink($oldLink: String, $newLink: String){
updateLink(input: { perspectiveUUID: "${perspective.uuid}", oldLink: $oldLink, newLink: $newLink }) {
timestamp
}
}
`)
$: REMOVE_LINK = mutation(gql`
mutation RemoveLink($link: String){
removeLink(input: { perspectiveUUID: "${perspective.uuid}", link: $link })
}
`)
$: CREATE_EXPRESSION = mutation(gql`
mutation CreateExpression($languageAddress: String, $content: String) {
createExpression(input: { languageAddress: $languageAddress, content: $content})
}
`)
let linksStore
let constructionMenu
let languages = []
let languageIcons = {
'note-ipfs': 'note',
'url-iframe': 'link'
}
let constructorIconComponents = {}
let iconStates = {}
let content
let container
let zoom = 0
let translateX = 0
let translateY = 0
let isPanning = false
let isMovingExpression = false
let movingLink
let movingLinkOriginal
let isLinking = false
let linkingSource
let linkingCursor = {}
let dropMove = false
let dropMoveTarget
let showSettings = false
let showExpressionBrwoser = false
$: if(content && zoom!=undefined && translateX!=undefined && translateY!=undefined) {
console.debug("SET TRANSFORM:", zoom)
content.setAttribute("style", calcPerspectiveContentStyle())
}
function calcPerspectiveContentStyle() {
return `
position: absolute;
width: 100%; height: 100%;
top: 0; left: 0;
transform: translateX(${translateX}px) translateY(${translateY}px) translateZ(${zoom}px);
transform-style: preserve-3d;
`
}
function handleMouseWheel(event) {
let factor = 0.3
const normalized = Math.abs(zoom/3)
if(zoom < 0 && normalized > 2) {
factor = 0.3 * Math.log2(normalized)
}
zoom -= event.deltaY * factor
if(zoom > 45) zoom = 45
console.log("zoom:", zoom)
}
function zoomNormalizedMouseMove(coords) {
let factor
if(zoom < 0) {
const dist = -zoom
factor = 1 + (dist/1000)
} else {
const dist = zoom
factor = 1 - (dist/1000)
}
return {
x: coords.x * factor,
y: coords.y * factor,
}
}
function dist(c1, c2) {
const x = c1.x - c2.x
const y = c1.y - c2.y
return Math.sqrt(x*x + y*y)
}
function hoveredLink(coords, moving) {
for(const i in $linksStore.data.links) {
const link = $linksStore.data.links[i]
if(link.data.source === "root" && link.data.predicate?.startsWith("coord2d://") && link.data.target != moving.target) {
const linkCoords = linkTo2D(link)
const d = dist(coords, linkCoords)
if(d < 300) return link
}
}
return null
}
function handleMouseMove(event) {
const d = zoomNormalizedMouseMove({x: event.movementX, y: event.movementY })
if(isPanning) {
translateX += d.x
translateY += d.y
}
if(isMovingExpression) {
let point = linkTo2D(movingLink)
point.x += d.x
point.y += d.y
movingLink.data.predicate = coordToPredicate(point)
dropMoveTarget = hoveredLink(point, movingLink.data)
if(dropMoveTarget) {
dropMove = true
console.log("drop move!")
} else {
dropMove = false
}
//linksStore.update(movingLink)
}
if(isLinking) {
linkingCursor = zoomNormalizedMouseMove({
x: event.clientX,
y: event.clientY
})
console.log("linking cursor:", JSON.stringify(linkingCursor))
}
}
function getLinkIdFromPath(event) {
for(const i in event.path) {
console.log(event.path[i])
if(event.path[i].dataset && event.path[i].dataset.linkId !== undefined)
return event.path[i].dataset.linkId
}
return undefined
}
function handleMouseDown(event) {
if(event.button === 2) return
if(event.target == content || event.target == container)
isPanning = true
else {
console.log("clicked something else:", event.path)
const linkId = getLinkIdFromPath(event)
console.log("link id:", linkId)
if(linkId) {
const hoveredLink = $linksStore.data.links.find(l => hashLinkExpression(l) == linkId)
if(isLinking) {
const newLink = {
source: linkingSource.data.target,
target: hoveredLink.data.target,
}
ADD_LINK({
variables: {
link: JSON.stringify(newLink)
}
})
} else {
if(hoveredLink) {
movingLink = {
author: { did: hoveredLink.author.did },
timestamp: hoveredLink.timestamp,
data: {
source: hoveredLink.data.source,
predicate: hoveredLink.data.predicate,
target: hoveredLink.data.target,
}
}
movingLinkOriginal = JSON.parse(JSON.stringify(movingLink))
isMovingExpression = true
console.log("MOVING")
}
else {
console.error("Couldn't find link with ID", linkId)
console.error("have links", $linksStore)
}
}
}
}
}
function handleMouseUp(event) {
console.debug(isMovingExpression, movingLink, movingLinkOriginal)
if(isMovingExpression && !linkEqual(movingLink, movingLinkOriginal)) {
let newLinkObject = JSON.parse(JSON.stringify(movingLink))
if(dropMove) {
newLinkObject.data.source = dropMoveTarget.data.target
}
delete newLinkObject.id
console.debug("Updating link:", JSON.stringify(movingLinkOriginal), JSON.stringify(movingLinkOriginal))
UPDATE_LINK({
variables: {
oldLink: JSON.stringify(movingLinkOriginal),
newLink: JSON.stringify(newLinkObject)
}
})
}
isPanning = false
isMovingExpression = false
isLinking = false
linkingSource = false
}
function contextMenu(event) {
isPanning = false
isMovingExpression = false
constructionMenu.open(event.clientX, event.clientY)
}
gqlClient.query({
query: LANGUAGES,
variables: { filter: "expressionUI" }
}).then( expressionUILanguages => {
console.log("Got installed languages:", JSON.stringify(expressionUILanguages))
languages = expressionUILanguages.data.languages
})
async function commitExpression(lang, content, container) {
const creationResult = await CREATE_EXPRESSION({
variables: {
languageAddress: lang.address,
content: JSON.stringify(content)
}
})
const exprURL = creationResult.data.createExpression
console.log("Created new expression:", exprURL)
ADD_LINK({
variables: {
link: JSON.stringify(new Link({source: 'root', target: exprURL}))
}
})
container.innerHTML = ''
}
async function createExpression(lang) {
console.log("Create expression:", lang, JSON.stringify(lang))
if(!constructorIconComponents[lang.name]) {
const { data } = await gqlClient.query({
query: gql`
{
language(address: "${lang.address}") {
constructorIcon {
code
}
}
}
`
})
const code = data.language.constructorIcon.code
const ConstructorIcon = iconComponentFromString(code, lang.name)
constructorIconComponents[lang.name] = ConstructorIcon
customElements.define(lang.name+"-constructor", ConstructorIcon);
}
const container = document.getElementById("constructor-container")
container.innerHTML = ''
const constructorIcon = new constructorIconComponents[lang.name]()
constructorIcon.commitExpression = async (content) => {
commitExpression(lang, content, container)
}
constructorIcon.discard = () => {
container.innerHTML = ''
}
container.appendChild(constructorIcon)
}
$: if(perspective) {
linksStore = query(ALL_LINKS_QUERY)
}
let expressionContextMenu
function onExpressionContextMenu(event) {
const { expressionURL, mouseEvent, parentLink } = event.detail
expressionContextMenu.open(mouseEvent.clientX, mouseEvent.clientY, expressionURL, parentLink)
isPanning = false
isMovingExpression = false
}
function onExpressionSwitchHeaderContent(event) {
const expression = event.detail
if(iconStates[expression] != 'rotated') {
iconStates[expression] = 'rotated'
} else {
iconStates[expression] = ''
}
isPanning = false
isMovingExpression = false
}
function onDeleteExpression(event) {
const expression = event.detail
$linksStore.data.links.forEach(l => {
if(l.data.target === expression) {
REMOVE_LINK({
variables: {
link: JSON.stringify(l)
}
})
}
})
}
function onLinkExpression(event) {
const link = event.detail
isLinking = true
linkingSource = link
}
</script>
<h1>${perspective.uuid}</h1>
<div class="perspective-container"
on:mousewheel={handleMouseWheel}
on:mousemove={handleMouseMove}
on:mousedown={handleMouseDown}
on:mouseup={handleMouseUp}
on:contextmenu={contextMenu}
bind:this={container}
>
<div class="perspective-content" bind:this={content}>
<ul class="inline">
{#if $linksStore.loading}
<li>Loading...</li>
{:else if $linksStore.error}
<li>ERROR: {$linksStore.error.message}</li>
{:else}
{#each $linksStore.data.links as link}
{#if link.data.source === 'root'}
{#if isMovingExpression && movingLink && linkEqual(link, movingLinkOriginal)}
<li class="inline expression-list-container"
style={`position: absolute; transform: translateX(${linkTo2D(movingLink).x}px) translateY(${linkTo2D(movingLink).y}px);`}
data-link-id={hashLinkExpression(link)}
>
<div class="drop-move-container" style={`transform: translateZ(${dropMove ? -2000 : 0}px);`}>
<ExpressionIcon expressionURL={link.data.target} perspectiveUUID={perspective.uuid}/>
</div>
</li>
{:else}
<li class="inline expression-list-container"
style={`position: absolute; transform: translateX(${linkTo2D(link).x}px) translateY(${linkTo2D(link).y}px);`}
data-link-id={hashLinkExpression(link)}
>
<ExpressionIcon
expressionURL={link.data.target}
parentLink={link}
perspectiveUUID={perspective.uuid}
on:context-menu={onExpressionContextMenu}
rotated={iconStates[link.data.target] === 'rotated'}
selected={linkingSource?.data?.target === link.data.target}>
</ExpressionIcon>
</li>
{/if}
{/if}
{/each}
{/if}
</ul>
<div id="constructor-container"></div>
{#if isLinking && linkingSource && linkingCursor.x && linkingCursor.y}
<svg class="link-path" width="100000" height="100000">
<path d={`M${linkTo2D(linkingSource).x+10000+400},${linkTo2D(linkingSource).y+10000} L${linkingCursor.x-5+10000},${linkingCursor.y-5+10000} Z`} stroke="red" stroke-width="3" fill="none"/>
</svg>
{/if}
</div>
</div>
<div id="side-bar-container">
<div class="side-bar-panel panel-1" style={`transform: rotateY(${showExpressionBrwoser? '90deg' : '0' })`}>
<div class="side-bar-button" on:click={() => showExpressionBrwoser = !showExpressionBrwoser}>
<span class="float-right"><Icon class="material-icons">web</Icon></span>
<Label>Expression Browser</Label>
</div>
<div class="side-panel-content expression-browser-panel" style={`opacity: ${showExpressionBrwoser? '1' : '0'}`}>
<ExpressionBrowser
on:close={()=> showExpressionBrwoser = false}
on:link-expresson={event => ADD_LINK({variables: {
link: JSON.stringify({
source: 'root',
target: event.detail
})
}})}
></ExpressionBrowser>
</div>
</div>
<div class="side-bar-panel panel-2" style={`transform: rotateY(${showSettings? '90deg' : '0' })`}>
<div class="side-bar-button" on:click={() => showSettings = !showSettings}>
<span class="float-right"><Icon class="material-icons">settings</Icon></span>
<Label>Perspective Settings</Label>
</div>
<div class="side-panel-content settings-panel" style={`opacity: ${showSettings? '1' : '0'}`}>
<PerspectiveSettings perspective={JSON.parse(JSON.stringify(perspective))}
on:submit={()=> {
showSettings = false
}}
on:cancel={()=> {
showSettings = false
}}>
</PerspectiveSettings>
</div>
</div>
</div>
<ConstructionMenu bind:this={constructionMenu}
languages={languages}
languageIcons={languageIcons}
on:language-clicked={({detail: lang}) => createExpression(lang)}
></ConstructionMenu>
<ExpressionContextMenu bind:this={expressionContextMenu}
on:switch-header-content={onExpressionSwitchHeaderContent}
on:delete={onDeleteExpression}
on:link={onLinkExpression}
></ExpressionContextMenu>
<style>
.perspective-container {
position: absolute;
top: 30px;
left: 0;
right: 0;
height: 100%;
perspective: 1000px;
transform-style: preserve-3d;
}
#side-bar-container {
position: absolute;
right: 0;
top: 0;
perspective: 100px;
perspective-origin: rightpo;
}
.side-bar-panel {
position: absolute;
right: 0;
transform-style: preserve-3d;
transition: transform 0.5s;
}
.panel-1 {
top: 115px;
}
.panel-2 {
top: 425px;
}
.expression-browser-panel {
height: 545px;
width: 380px;
}
.settings-panel {
width: 204px;
}
.side-bar-button {
position: absolute;
width: 175px;
height: 45px;
padding: 10px 50px 0 25px;
transform-origin: right bottom;
transform: rotate(-90deg) translateX(115px);
right: 0;
background-color: white;
border: 1px solid rgb(127, 129, 255);
color: rgb(127, 129, 255);
cursor: pointer;
}
.float-right {
float: right;
}
.side-panel-content {
position: absolute;
transform: rotateY(-90deg) translateX(-300px) translateZ(255px);
transition: opacity 0.5s;
padding: 40px;
border: 1px solid rgb(127, 129, 255);
background-color: white;
}
.debug {
position: fixed;
width: 100%;
z-index: -10;
}
.inline {
display: inline;
transform-style: preserve-3d;
}
.link-path {
position: absolute;
top: 0;
left: 0;
transform: translateX(-10000px) translateY(-10000px);
}
.drop-move-container {
transition: transform 0.5s;
}
</style>