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
|
import { createGeoLink } from './dumbymap';
import { scrollToBlock } from './dumbyUtils';
class Item extends HTMLDivElement {
constructor({ text, innerHTML, onclick }) {
super();
this.innerHTML = innerHTML ?? text;
this.onclick = onclick;
this.classList.add('menu-item');
}
}
window.customElements.define('menu-item', Item, { extends: 'div' });
class Folder extends HTMLDivElement {
constructor({ text, innerHTML, items }) {
super();
this.innerHTML = innerHTML ?? text;
this.classList.add('folder', 'menu-item');
this.items = items;
this.onmouseover = () => {
if (this.querySelector('.sub-menu')) return;
// Prepare submenu
const submenu = document.createElement('div');
submenu.className = 'sub-menu';
submenu.style.cssText = `position: absolute; left: 105%; top: 0px;`;
this.items.forEach(item => submenu.appendChild(item));
// hover effect
this.parentElement
.querySelectorAll('.sub-menu')
.forEach(sub => sub.remove());
this.appendChild(submenu);
};
}
}
window.customElements.define('menu-folder', Folder, { extends: 'div' });
export const pickMapItem = dumbymap =>
new Folder({
innerHTML: '<span>Maps<span><span class="info">(Tab)</span>',
items: dumbymap.utils.renderedMaps().map(
map =>
new Item({
text: map.id,
onclick: () => {
map.classList.add('focus');
map.scrollIntoView({ behavior: 'smooth' });
},
}),
),
});
export const pickBlockItem = dumbymap =>
new Folder({
innerHTML: '<span>Blocks<span><span class="info">(n/p)</span>',
items: dumbymap.blocks.map(
(block, index) =>
new Item({
text:
`<strong>(${index})</strong>` +
block
.querySelector('p')
?.textContent.substring(0, 15)
.concat(' ...'),
onclick: () => {
block.classList.add('focus');
scrollToBlock(block);
},
}),
),
});
export const pickLayoutItem = dumbymap =>
new Folder({
innerHTML: '<span>Layouts<span><span class="info">(x)</span>',
items: [
new Item({
text: 'EDIT',
onclick: () =>
dumbymap.container
.closest('[data-mode]')
.setAttribute('data-mode', 'editing'),
}),
...dumbymap.layouts.map(
layout =>
new Item({
text: layout.name,
onclick: () =>
dumbymap.container.setAttribute('data-layout', layout.name),
}),
),
],
});
export class GeoLink {
constructor({ range }) {
this.range = range;
}
createElement = () => {
const element = document.createElement('div');
element.className = 'menu-item';
element.innerText = 'Add GeoLink';
element.onclick = this.addGeoLinkbyRange;
return element;
};
addGeoLinkbyRange = () => {
const range = this.range;
const content = range.toString();
// FIXME Apply geolink only on matching sub-range
const match = content.match(/(^\D*[\d.]+)\D+([\d.]+)\D*$/);
if (!match) return false;
const [x, y] = match.slice(1);
const anchor = document.createElement('a');
anchor.textContent = content;
// FIXME apply WGS84
anchor.href = `geo:${y},${x}?xy=${x},${y}`;
if (createGeoLink(anchor)) {
range.deleteContents();
range.insertNode(anchor);
}
};
}
export class Suggestion {
constructor({ text, replace }) {
this.text = text;
this.replace = replace;
}
createElement(codemirror) {
const option = document.createElement('div');
if (this.text.startsWith('<')) {
option.innerHTML = this.text;
} else {
option.innerText = this.text;
}
option.classList.add('container__suggestion');
option.onmouseover = () => {
Array.from(option.parentElement?.children)?.forEach(s =>
s.classList.remove('focus'),
);
option.classList.add('focus');
};
option.onmouseout = () => {
option.classList.remove('focus');
};
option.onclick = () => {
const anchor = codemirror.getCursor();
codemirror.setSelection(anchor, { ...anchor, ch: 0 });
codemirror.replaceSelection(this.replace);
codemirror.focus();
const newAnchor = { ...anchor, ch: this.replace.length };
codemirror.setCursor(newAnchor);
};
return option;
}
}
export const renderResults = (dumbymap, map) =>
new Item({
text: 'Render Results',
onclick: e => {
const modal = dumbymap.modal;
modal.open();
modal.overlayBlur = 3;
modal.closeByEscKey = false;
// HACK find another way to override inline style
document.querySelector('.plainmodal-overlay-force').style.position =
'relative';
map.renderer.results.forEach(result =>
printObject(
result,
dumbymap.modalContent,
`${result.func.name} (${result.state})`,
),
);
},
});
function printObject(obj, parentElement, name) {
const detailsEle = document.createElement('details');
const details = name ?? Object.values(obj)[0];
detailsEle.innerHTML = `<summary>${details}</summary`;
parentElement.appendChild(detailsEle);
detailsEle.onclick = () => {
if (detailsEle.children.length > 1) return;
Object.entries(obj).forEach(([key, value]) => {
if (typeof value === 'object') {
printObject(value, detailsEle, key);
} else {
let valueString =
typeof value === 'function'
? `<pre>${value}</pre>`
: value ?? typeof value;
const propertyElement = document.createElement('p');
propertyElement.innerHTML = `<strong>${key}</strong>: ${valueString}`;
detailsEle.appendChild(propertyElement);
}
});
};
}
|