Newer
Older
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
import { PanZoom } from "@sc07-canvas/lib/src/renderer/PanZoom";
import { Canvas } from "./canvas";
import throttle from "lodash.throttle";
import EventEmitter from "eventemitter3";
const CLIENT_PARAMS = {
canvas_x: "x",
canvas_y: "y",
canvas_zoom: "zoom",
template_url: "tu",
template_width: "tw",
template_x: "tx",
template_y: "ty",
};
export interface IRouterData {
canvas?: {
x: number;
y: number;
zoom?: number;
};
template?: {
url: string;
width?: number;
x?: number;
y?: number;
};
}
interface RouterEvents {
navigate(route: IRouterData): void;
}
class _Router extends EventEmitter<RouterEvents> {
PanZoom: PanZoom | undefined;
// React TemplateContext
templateState: {
enabled: boolean;
width?: number;
x: number;
y: number;
url?: string;
} = {
enabled: false,
x: 0,
y: 0,
};
constructor() {
super();
window.addEventListener("hashchange", this._hashChange.bind(this));
}
destroy() {
// NOTE: this method *never* gets called because this is intended to be global
window.removeEventListener("hashchange", this._hashChange.bind(this));
}
_hashChange(e: HashChangeEvent) {
const data = this.get();
console.info("[Router] Navigated", data);
this.emit("navigate", data);
}
queueUpdate = throttle(this.update, 500);
update() {
const url = this.getURL();
if (!url) return;
console.log("[Router] Updating URL");
window.history.replaceState({}, "", url);
}
getURL() {
const canvas = Canvas.instance;
// this is not that helpful because the data is more spread out
// this gets replaced by using TemplateContext data
// const template = Template.instance;
if (!canvas) {
console.warn("Router#update called but no canvas instance exists");
return;
}
if (!this.PanZoom) {
console.warn("Router#update called but no PanZoom instance exists");
}
const params = new URLSearchParams();
const position = canvas.panZoomTransformToCanvas();
params.set(CLIENT_PARAMS.canvas_x, position.canvasX + "");
params.set(CLIENT_PARAMS.canvas_y, position.canvasY + "");
params.set(
CLIENT_PARAMS.canvas_zoom,
(this.PanZoom!.transform.scale >> 0) + ""
);
if (this.templateState.enabled && this.templateState.url) {
params.set(CLIENT_PARAMS.template_url, this.templateState.url + "");
if (this.templateState.width)
params.set(CLIENT_PARAMS.template_width, this.templateState.width + "");
params.set(CLIENT_PARAMS.template_x, this.templateState.x + "");
params.set(CLIENT_PARAMS.template_y, this.templateState.y + "");
}
return (
window.location.protocol + "//" + window.location.host + "/#" + params
);
}
/**
* Parse the URL and return what was found, following specifications
* There's no defaults, if it's not specified in the url, it's not specified in the return
*
* @returns
*/
get(): IRouterData {
const params = new URLSearchParams(window.location.hash.slice(1));
let canvas:
| {
x: number;
y: number;
zoom?: number;
}
| undefined = undefined;
if (
params.has(CLIENT_PARAMS.canvas_x) &&
params.has(CLIENT_PARAMS.canvas_y)
) {
// params exist, now to validate
// x & y or nothing; zoom is optional
let x = parseInt(params.get(CLIENT_PARAMS.canvas_x) || "");
let y = parseInt(params.get(CLIENT_PARAMS.canvas_y) || "");
if (!isNaN(x) && !isNaN(y)) {
// x & y are valid numbers
canvas = {
x,
y,
};
if (params.has(CLIENT_PARAMS.canvas_zoom)) {
let zoom = parseInt(params.get(CLIENT_PARAMS.canvas_zoom) || "");
if (!isNaN(zoom)) {
canvas.zoom = zoom;
}
}
}
}
let template:
| {
url: string;
width?: number;
x?: number;
y?: number;
}
| undefined = undefined;
if (params.has(CLIENT_PARAMS.template_url)) {
const url = params.get(CLIENT_PARAMS.template_url)!;
template = { url };
if (params.has(CLIENT_PARAMS.template_width)) {
let width = parseInt(params.get(CLIENT_PARAMS.template_width) || "");
if (!isNaN(width)) {
template.width = width;
}
}
if (
params.has(CLIENT_PARAMS.template_x) &&
params.has(CLIENT_PARAMS.template_y)
) {
// both x & y has to be set
let x = parseInt(params.get(CLIENT_PARAMS.template_x) || "");
let y = parseInt(params.get(CLIENT_PARAMS.template_y) || "");
if (!isNaN(x) && !isNaN(y)) {
template.x = x;
template.y = y;
}
}
}
return {
canvas,
template,
};
}
/**
* Accept updates to local copy of TemplateContext from React
* @param args
*/
setTemplate(args: {
enabled: boolean;
width?: number;
x: number;
y: number;
url?: string;
}) {
this.templateState = args;
}
}
export const Router = new _Router();