-
Notifications
You must be signed in to change notification settings - Fork 1
/
addNavInteraction.js
125 lines (99 loc) · 3.54 KB
/
addNavInteraction.js
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
import {
createListener,
selectElementContents,
blurTargetOnEnter,
checkCharacterCount,
} from "./utils.js";
export async function addNavInteraction(nav, state) {
const listen = createListener(nav);
let tcTitle = document.getElementById("title-field");
listen("pointerdown", ".download", (e) => {
const x = state.panZoom ? state.panZoom.x() : 0;
const y = state.panZoom ? state.panZoom.y() : 0;
const scale = state.panZoom ? state.panZoom.scale() : 1;
const tools = Object.fromEntries(
Object.entries(state.toolchain.tools).map(([toolID, tool]) => {
// Can't serialize the lifecycle methods, use destructuring to get rid of them
const toolObj = state.imports[tool.path];
const { lifecycle: x, inports: y, outports: z, ...newTool } = tool;
// We don't need to save inport and outport values, because they are derived
// from state.So we save them as their initial config values
newTool.inports = toolObj.config.inports ?? {};
newTool.outports = toolObj.config.outports ?? {};
return [toolID, newTool];
})
);
const toolchainJSON = {
title: state.toolchain.title ?? untitled,
pipes: state.toolchain.pipes,
tools: tools,
workspace: {
x: x,
y: y,
scale: scale,
},
};
const downloadLink = document.createElement("a");
downloadLink.href =
"data:text/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(toolchainJSON));
downloadLink.download = (state.toolchain.title ?? "toolchain") + ".json";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
});
function upload(e) {
let file = e.target.files[0];
const fileReader = new FileReader();
fileReader.readAsText(file);
fileReader.onload = () => {
state.uploadToolchain(JSON.parse(fileReader.result));
};
}
listen("pointerdown", ".upload", (e) => {
let fileInputElement = document.createElement("input");
fileInputElement.setAttribute("type", "file");
fileInputElement.style.display = "none";
document.body.appendChild(fileInputElement);
fileInputElement.click();
fileInputElement.onchange = upload;
document.body.removeChild(fileInputElement);
});
async function importExample(e) {
const { default: toolchainJSON } = await state.examples[
e.target.dataset.path
]();
state.uploadToolchain(toolchainJSON);
}
async function importSnippet(e) {
const { default: toolchainJSON } = await state.snippets[
e.target.dataset.path
]();
state.uploadToolchain(toolchainJSON, true);
}
listen("pointerdown", ".ex", importExample);
listen("pointerdown", ".snip", importSnippet);
listen("pointerdown", ".debug", (e) => {
state.debug = !state.debug;
});
function updateToolchainName(e) {
state.toolchain.title = tcTitle.textContent;
tcTitle.contentEditable = false;
tcTitle.removeEventListener("focusout", updateToolchainName);
tcTitle.removeEventListener("keypress", blurTargetOnEnter);
tcTitle.removeEventListener("keypress", check);
}
function check(e) {
checkCharacterCount(tcTitle, 50, e);
}
listen("pointerdown", ".edit-name", (e) => {
tcTitle.contentEditable = true;
tcTitle.addEventListener("focusout", updateToolchainName);
tcTitle.addEventListener("keypress", blurTargetOnEnter);
tcTitle.addEventListener("keypress", check);
setTimeout(function () {
selectElementContents(tcTitle);
tcTitle.focus();
}, 0);
});
}