-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
540 lines (477 loc) · 22 KB
/
index.html
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
<html class="light">
<head>
<meta charset="utf-8" lang="en">
<title>Markdeep Diagram Drafting Board</title>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="apple-touch-icon" href="favicon.png">
<style>
* {
box-sizing: border-box;
}
html {
/* ridiculous values, to be overwritten via js */
--font-size: 15px;
--font-stack: "Comic Sans MS";
--sidebar-width: 17rem;
/* default values (must match "data-default" attributes) */
--zoom-level: 1;
--input-line-height: 1.25em;
--input-letter-spacing: 0.15px;
font-size: var(--font-size);
font-family: var(--font-stack);
}
html.dark {
filter: invert(100%) hue-rotate(180deg);
}
body {
margin: 0;
padding: 0;
background-color: #eee;
}
/* SIDEBAR */
aside {
width: var(--sidebar-width);
background-color: #ccc;
padding: 1em;
position: absolute;
max-height: 100%;
overflow-y: scroll;
}
h1 {
font: inherit !important;
font-size: 2em !important;
border: 0 !important;
margin: 0 !important;
padding: 0 !important;
}
h1:before {
content: "" !important;
margin: 0 !important;
}
p {
margin: 1em 0;
}
a {
font: inherit !important;
color: #666 !important;
text-decoration: underline !important;
}
hr {
border: none;
border-top: 1px solid #aaa;
margin: 1rem 0;
}
button, label {
font: inherit;
background-color: #eee;
border: 1px solid #aaa;
text-align: left;
margin: 1em 0 0.3em 0;
padding: 0.5em;
display: block;
width: 100%;
text-transform: uppercase;
}
button:hover {
color: white;
background-color: #444;
border-color: #eee;
}
button b {
float: right;
height: 0;
margin-top: -0.3em;
}
input[type="range"] {
width: 100%;
margin: 0;
}
label b {
font-weight: normal;
float: right;
color: #888;
}
span {
font-size: 0.85em;
display: block;
}
a.reset {
display: inline-block;
text-transform: uppercase;
text-decoration: none !important;
font-size: 0.6em !important;
letter-spacing: 1px;
}
/* STATUS */
footer {
position: absolute;
left: 0;
bottom: 0;
width: var(--sidebar-width);
background-color: #ddd;
padding: 1em;
font-size: 0.8em;
pointer-events: none;
opacity: 1;
}
footer.hidden {
opacity: 0;
transition: opacity 0.5s ease-out;
}
/* INPUT/OUTPUT */
section {
width: calc(100vw - var(--sidebar-width));
height: 100vh;
margin-left: var(--sidebar-width);
background-color: white;
}
textarea {
padding: 1em;
margin: 0;
width: 100%;
height: 100%;
border: 0px;
background-color: transparent;
outline: none;
resize: none;
font: inherit;
color: #ccc;
caret-color: #000;
font-size: calc(1em * var(--zoom-level));
line-height: calc(var(--input-line-height));
letter-spacing: calc(var(--input-letter-spacing));
}
figure {
margin: 0;
margin-top: calc(-100vh + (-8px + 1em) * var(--zoom-level));
margin-left: calc((-4px + 1em) * var(--zoom-level));
}
svg.diagram {
pointer-events: none;
transform-origin: top left;
transform: scale(var(--zoom-level));
}
/* GITHUB CORNER */
@keyframes octocat-wave {
0%, 100% {
transform:rotate(0);
}
20%, 60% {
transform:rotate(-25deg);
}
40%, 80% {
transform:rotate(10deg);
}
}
.github-corner:hover .octo-arm {
animation:octocat-wave 560ms ease-in-out;
}
.github-corner svg {
--github-corner-size: 4rem;
fill:#888;
color:#ccc;
position: absolute;
top: 0;
left: calc(var(--sidebar-width) - var(--github-corner-size));
width: var(--github-corner-size);
height: var(--github-corner-size);
}
.github-corner svg:hover {
fill: #555 !important;
}
</style>
</head>
<body class="md">
<aside>
<h1>Markdeep Diagram <em>Drafting Board</em></h1>
<p>This tool enables you to draft <a href="https://casual-effects.com/markdeep/features.md.html#basicformatting/diagrams">Markdeep diagrams</a> with a <strong>live-updating preview overlaid onto your source code</strong>.</p>
<hr>
<label><input type="checkbox" onclick="togglePreview(this);" checked> show preview</label>
<span>Hides or shows the superimposed preview.</span>
<button onclick="copySource(this);">copy source<b></b></button>
<span>The copied source will be surrounded with asterisks, ready for pasting into a Markdeep document <em>(or back into this tool, which will strip the asterisks on paste to ease further editing)</em>.</span>
<button onclick="downloadSVG();">download svg<b></b></button>
<span>Handy for inserting your diagram into a non-Markdeep document. <em>Includes the source in a <metadata> tag.</em></span>
<hr>
<label><input type="checkbox" onclick="toggleDarkMode(this);" data-default="light" class="dark-mode-checkbox"> dark mode</label>
<label>
<input type="range" min="0.5" max="3.0" step="0.01" value="1.0" data-default="1.0" class="zoom-level-slider" oninput="adjustZoomLevel(this.value)">
<br>zoom level <b>100%</b>
</label>
<span>Scales both source and preview.</span>
<label>
<input type="range" min="1.00" max="1.50" step="0.01" value="1.25" data-default="1.25" class="line-height-slider" oninput="adjustLineHeight(this.value)">
<br>line height <b>1.25em</b>
</label>
<label>
<input type="range" min="-0.30" max="0.60" step="0.01" value="0.15" data-default="0.15" class="letter-spacing-slider" oninput="adjustLetterSpacing(this.value)">
<br>letter spacing <b>0.15px</b>
</label>
<span>Both of the above might need adjusting depending on your zoom level, browser and installed fonts.</span>
<p><span><em>Your source and these settings are continually written to local storage. They will be restored when you close and reopen this page.</em></span></p>
<a class="reset" href="javascript:resetSettings();">reset settings</a>
</aside>
<footer class="hidden"></footer>
<section>
<textarea> ____ *
| |_____.---. |
o _____| )----------)-------.
/ \ | '---' | __|__
/___\ | | \ /
| '-------------. | \ /
A₁ ----------------' | | o
.-------------------. o-----)-------' |
| |___.---. | |___.---.
A₂ ---*---.__.---. ___| )--*--.__..---. ____) )----- Y₁
__| o----*--' '---' ______)) )---' '---'
B -------' '---' | | ''---'
| o
| / \
| /___\
| |
'--------------'
^
|
EDIT THIS DIAGRAM</textarea>
<figure><svg class="diagram"></svg></figure>
</section>
<script>window.markdeepOptions = {mode: "script"};</script>
<script src="https://casual-effects.com/markdeep/latest/markdeep.min.js" charset="utf-8"></script>
<script src="extractDiagram.js"></script>
<script>
document.head.innerHTML = document.head.innerHTML + window.markdeep.stylesheet();
// helper function for displaying status messages
function setStatus(message) {
var status = document.querySelector("footer");
status.innerHTML = message;
status.className = "";
setTimeout(function () {
var status = document.querySelector("footer");
status.className = "hidden";
}, 200);
}
var input = document.querySelector("textarea");
var output = document.querySelector("figure");
var darkModeCheckbox = document.querySelector(".dark-mode-checkbox");
var zoomLevelSlider = document.querySelector(".zoom-level-slider");
var lineHeightSlider = document.querySelector(".line-height-slider");
var letterSpacingSlider = document.querySelector(".letter-spacing-slider");
// set font size and restore settings from local storage
function setup() {
// set font size based on what markdeep decides to use for diagrams
var dummyDiagram = document.querySelector("svg.diagram");
var fontStack = window.getComputedStyle(dummyDiagram).getPropertyValue("font-family");
var fontSize = window.getComputedStyle(dummyDiagram).getPropertyValue("font-size");
document.documentElement.style.setProperty("--font-stack", fontStack);
document.documentElement.style.setProperty("--font-size", fontSize);
// restore state from local storage
if (inputSource = window.localStorage.getItem("input-source")) { // purposefully not catching the empty case
input.value = inputSource;
render();
}
if (darkMode = window.localStorage.getItem("dark-mode")) {
darkModeCheckbox.checked = darkMode == "dark";
toggleDarkMode(darkModeCheckbox);
}
if (zoomLevel = window.localStorage.getItem("zoom-level")) {
zoomLevelSlider.value = zoomLevel;
adjustZoomLevel(zoomLevel);
}
if (lineHeight = window.localStorage.getItem("input-line-height")) {
lineHeightSlider.value = lineHeight;
adjustLineHeight(lineHeight);
}
if (letterSpacing = window.localStorage.getItem("input-letter-spacing")) {
letterSpacingSlider.value = letterSpacing;
adjustLetterSpacing(letterSpacing);
}
}
setup();
// take input and convert it to a diagram, superimpose that on the input
function render() {
setStatus("Saving and rendering preview...")
var inputSource = input.value;
window.localStorage.setItem("input-source", inputSource);
var diagramSource = window.markdeep.formatDiagram(inputSource);
output.innerHTML = diagramSource;
}
input.addEventListener('keyup', render);
// via https://stackoverflow.com/a/11077016
function insertAtCursor(myField, myValue) {
if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
// on paste, extract diagram from clipboard if present and insert it
// into the input without marker asterisks (if the clipboard doesn't
// seem to contain a diagram, do nothing special)
input.addEventListener('paste', (event) => {
var paste = (event.clipboardData || window.clipboardData).getData("text");
var diagram = extractDiagram("\n" + paste + "\n");
if (diagram.diagramString) {
insertAtCursor(input, diagram.diagramString);
event.preventDefault();
}
});
function togglePreview(checkbox) {
if (checkbox.checked) {
output.style.display = "block";
} else {
output.style.display = "none";
}
}
// copy source (optionally with Markdeep diagram marker asterisks) to the clipboard
function copySource(button, withAsterisks = true, padding = 0) {
setStatus("Copying to the clipboard...")
//setStatus("Copying to the clipboard" + (withAsterisks ? " (with asterisks)" : "") + "...")
// make a backup of the current value (if withAsterisks is true, we'll eventually need to restore this)
var inputSourceBackup = input.value;
if (withAsterisks) {
var lines = input.value.split("\n");
// determine length of longest line
var longestLineLength = 0;
lines = lines.map(function(line) {
line = line.replace("\n", "");
if (line.length > longestLineLength) {
longestLineLength = line.length;
}
return line;
});
// pad with spaces and insert leading and trailing asterisks
lines = lines.map(line => "*" + " ".repeat(padding) + line.padEnd(longestLineLength, " ") + " ".repeat(padding) + "*");
// insert top and bottom rows of asterisks
var topBottom = "*".repeat(longestLineLength + 2 + 2 * padding);
lines.splice(0, 0, topBottom);
lines.push(topBottom);
input.value = lines.join("\n");
}
input.focus();
input.select();
var successful = false;
try {
successful = document.execCommand("copy");
} catch (err) {
console.log("Unable to copy to clipboard: " + err);
}
button.querySelector("b").innerHTML = successful ? "✅" : "❌";
setTimeout(function() {
button.querySelector("b").innerHTML = "";
}, 1000);
// restore backup
input.value = inputSourceBackup;
}
function downloadSVG() {
setStatus("Preparing SVG for download...")
var diagramElement = output.querySelector("svg");
// extract computed values for properties that Markdeep determines dynamically
var computedStyle = window.getComputedStyle(diagramElement);
var computedFontFamily = computedStyle.getPropertyValue("font-family");
var computedFontSize = computedStyle.getPropertyValue("font-size");
var computedStrokeWidth = computedStyle.getPropertyValue("stroke-width");
// make a copy of the rendered diagram and apply both extracted "dynamic" styles and "static" styles from the Markdeep source code (version 1.09 – might need updating in the future)
var clonedDiagramElement = diagramElement.cloneNode(true);
clonedDiagramElement.style += `
display: block;
font-family: ${computedFontFamily};
font-size: ${computedFontSize};
text-align: center;
stroke-linecap: round;
stroke-width: ${computedStrokeWidth};
page-break-inside: avoid;
stroke: #000;
fill: #000;
`;
clonedDiagramElement.querySelectorAll(".opendot").forEach(n => {
n.style.setProperty("fill", "#fff"); // weirdly, extending n.style using the += operator doesn't seem to work here
});
clonedDiagramElement.querySelectorAll(".shadeddot").forEach(n => {
n.style.setProperty("fill", "#ccc");
});
clonedDiagramElement.querySelectorAll(".dotteddot").forEach(n => {
n.style.setProperty("stroke", "#000");
n.style.setProperty("stroke-dasharray", "4");
n.style.setProperty("fill", "none");
});
clonedDiagramElement.querySelectorAll("text").forEach(n => {
n.style.setProperty("stroke", "none");
});
// for convenience, set the viewBox attribute based on the width and height attributes (which are now redundant, so let's remove them)
var w = clonedDiagramElement.getAttribute("width"),
h = clonedDiagramElement.getAttribute("height");
clonedDiagramElement.removeAttribute("width");
clonedDiagramElement.removeAttribute("height");
clonedDiagramElement.setAttribute("viewBox", "0 0 " + w + " " + h);
// include the Markdeep diagram source code
var inputSource = input.value;
var inputSourceElement = document.createElement("metadata");
inputSourceElement.innerHTML = inputSource;
clonedDiagramElement.appendChild(inputSourceElement);
// get thusly adjusted diagram source and indent it nicely, based on https://stackoverflow.com/a/49458964
var diagramSource = clonedDiagramElement.outerHTML;
var formatted = "",
indent = "";
diagramSource.split(/>\s*</).forEach(function(node) {
if (node.match(/^\/\w/)) indent = indent.substring(2);
formatted += indent + "<" + node + ">\n";
if (node.match(/^<?\w[^>]*[^\/]$/)) indent += " ";
});
diagramSource = formatted.substring(1, formatted.length-2);
// write diagram source to an invisible link's target, then click that link to initiate download, based on https://stackoverflow.com/a/18197341
var dummyLink = document.createElement("a");
dummyLink.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(diagramSource));
dummyLink.setAttribute("download", "diagram.svg");
dummyLink.style.display = "none";
document.body.appendChild(dummyLink);
dummyLink.click();
document.body.removeChild(dummyLink);
}
function toggleDarkMode(checkbox) {
if (checkbox.checked) {
document.documentElement.className = "dark";
window.localStorage.setItem("dark-mode", "dark");
} else {
document.documentElement.className = "light";
window.localStorage.setItem("dark-mode", "light");
}
}
function adjustZoomLevel(zoomLevel) {
document.documentElement.style.setProperty('--zoom-level', zoomLevel);
zoomLevelSlider.parentNode.querySelector("b").innerHTML = parseInt(100 * zoomLevel) + "%";
window.localStorage.setItem("zoom-level", zoomLevel);
}
function adjustLineHeight(lineHeight) {
document.documentElement.style.setProperty('--input-line-height', lineHeight + "em");
lineHeightSlider.parentNode.querySelector("b").innerHTML = lineHeight + "em";
window.localStorage.setItem("input-line-height", lineHeight);
}
function adjustLetterSpacing(letterSpacing) {
document.documentElement.style.setProperty('--input-letter-spacing', letterSpacing + "px");
letterSpacingSlider.parentNode.querySelector("b").innerHTML = letterSpacing + "px";
window.localStorage.setItem("input-letter-spacing", letterSpacing);
}
function resetSettings() {
darkModeCheckbox.checked = darkModeCheckbox.getAttribute("data-default") == "dark";
toggleDarkMode(darkModeCheckbox);
zoomLevelSlider.value = zoomLevelSlider.getAttribute("data-default");
adjustZoomLevel(zoomLevelSlider.value);
lineHeightSlider.value = lineHeightSlider.getAttribute("data-default");
adjustLineHeight(lineHeightSlider.value);
letterSpacingSlider.value = letterSpacingSlider.getAttribute("data-default");
adjustLetterSpacing(letterSpacingSlider.value);
}
</script>
<a href="https://github.com/doersino/markdeep-diagram-drafting-board" class="github-corner" aria-label="View source on GitHub">
<svg viewBox="0 0 250 250" aria-hidden="true">
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
<path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
<path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path>
</svg>
</a>
</body>
</html>