2024-09-08 13:53:29 +02:00
|
|
|
import { CodeEditor } from "rkgk/code-editor.js";
|
2024-09-05 21:30:07 +02:00
|
|
|
|
2024-08-10 23:13:20 +02:00
|
|
|
const defaultBrush = `
|
2024-09-01 10:09:23 +02:00
|
|
|
-- This is your brush.
|
2024-09-08 13:53:29 +02:00
|
|
|
-- Try playing around with the numbers,
|
2024-09-01 10:09:23 +02:00
|
|
|
-- and see what happens!
|
|
|
|
|
2024-10-23 21:20:40 +02:00
|
|
|
withDotter \\d ->
|
2024-09-08 13:53:29 +02:00
|
|
|
stroke 8 #000 (d To)
|
2024-08-10 23:13:20 +02:00
|
|
|
`.trim();
|
|
|
|
|
|
|
|
export class BrushEditor extends HTMLElement {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
this.classList.add("rkgk-panel");
|
|
|
|
|
2024-09-06 22:50:25 +02:00
|
|
|
this.codeEditor = this.appendChild(
|
|
|
|
new CodeEditor([
|
|
|
|
{
|
|
|
|
className: "layer-error-squiggles",
|
|
|
|
render: (code, element) => this.#renderErrorSquiggles(code, element),
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
);
|
2024-09-05 21:30:07 +02:00
|
|
|
this.codeEditor.setCode(localStorage.getItem("rkgk.brushEditor.code") ?? defaultBrush);
|
|
|
|
this.codeEditor.addEventListener(".codeChanged", (event) => {
|
2024-09-05 22:05:15 +02:00
|
|
|
localStorage.setItem("rkgk.brushEditor.code", event.newCode);
|
|
|
|
|
2024-08-10 23:13:20 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
Object.assign(new Event(".codeChanged"), {
|
2024-09-05 21:30:07 +02:00
|
|
|
newCode: event.newCode,
|
2024-08-10 23:13:20 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2024-08-19 23:56:11 +02:00
|
|
|
|
|
|
|
this.errorHeader = this.appendChild(document.createElement("h1"));
|
|
|
|
this.errorHeader.classList.add("error-header");
|
|
|
|
|
|
|
|
this.errorArea = this.appendChild(document.createElement("pre"));
|
|
|
|
this.errorArea.classList.add("errors");
|
2024-08-10 23:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get code() {
|
2024-09-05 21:30:07 +02:00
|
|
|
return this.codeEditor.code;
|
2024-08-10 23:13:20 +02:00
|
|
|
}
|
2024-08-19 23:56:11 +02:00
|
|
|
|
|
|
|
resetErrors() {
|
|
|
|
this.errorHeader.textContent = "";
|
|
|
|
this.errorArea.textContent = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
renderHakuResult(phase, result) {
|
|
|
|
this.resetErrors();
|
2024-09-06 22:50:25 +02:00
|
|
|
this.errorSquiggles = null;
|
2024-08-19 23:56:11 +02:00
|
|
|
|
2024-09-06 22:50:25 +02:00
|
|
|
if (result.status != "error") {
|
|
|
|
// We need to request a rebuild if there's no error to remove any squiggles that may be
|
|
|
|
// left over from the error state.
|
|
|
|
this.codeEditor.renderLayer("layer-error-squiggles");
|
|
|
|
return;
|
|
|
|
}
|
2024-08-19 23:56:11 +02:00
|
|
|
|
|
|
|
this.errorHeader.textContent = `${phase} failed`;
|
|
|
|
|
|
|
|
if (result.errorKind == "diagnostics") {
|
2024-09-06 22:50:25 +02:00
|
|
|
this.codeEditor.rebuildLineMap();
|
|
|
|
this.errorSquiggles = this.#computeErrorSquiggles(
|
|
|
|
this.codeEditor.lineMap,
|
|
|
|
result.diagnostics,
|
|
|
|
);
|
|
|
|
this.codeEditor.renderLayer("layer-error-squiggles");
|
|
|
|
|
2024-08-19 23:56:11 +02:00
|
|
|
this.errorArea.textContent = result.diagnostics
|
|
|
|
.map(
|
|
|
|
(diagnostic) => `${diagnostic.start}..${diagnostic.end}: ${diagnostic.message}`,
|
|
|
|
)
|
|
|
|
.join("\n");
|
|
|
|
} else if (result.errorKind == "plain") {
|
|
|
|
this.errorHeader.textContent = result.message;
|
|
|
|
} else if (result.errorKind == "exception") {
|
|
|
|
// TODO: This should show a stack trace.
|
|
|
|
this.errorArea.textContent = `an exception occurred: ${result.message}`;
|
|
|
|
} else {
|
|
|
|
console.warn(`unknown error kind: ${result.errorKind}`);
|
|
|
|
this.errorHeader.textContent = "(unknown error kind)";
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 22:50:25 +02:00
|
|
|
|
|
|
|
#computeErrorSquiggles(lineMap, diagnostics) {
|
|
|
|
// This is an extremely inefficient algorithm.
|
|
|
|
// If we had better control of drawing (I'm talking: letter per letter, with a shader!)
|
|
|
|
// this could be done lot more efficiently. But alas, HTML giveth, HTML taketh away.
|
|
|
|
|
|
|
|
// The first step is to determine per-line spans.
|
|
|
|
// Since we render squiggles by line, it makes the most sense to split the work up to a
|
|
|
|
// line by line basis.
|
|
|
|
|
|
|
|
let rawLineSpans = new Map();
|
|
|
|
|
|
|
|
for (let diagnostic of diagnostics) {
|
|
|
|
let firstLine = lineMap.lineIndexAt(diagnostic.start);
|
|
|
|
let lastLine = lineMap.lineIndexAt(diagnostic.start);
|
|
|
|
for (let i = firstLine; i <= lastLine; ++i) {
|
|
|
|
let bounds = lineMap.get(i);
|
|
|
|
let start = i == firstLine ? diagnostic.start - bounds.start : 0;
|
|
|
|
let end = i == lastLine ? diagnostic.end - bounds.start : bounds.end;
|
|
|
|
|
|
|
|
if (!rawLineSpans.has(i)) {
|
|
|
|
rawLineSpans.set(i, []);
|
|
|
|
}
|
|
|
|
let onThisLine = rawLineSpans.get(i);
|
|
|
|
onThisLine.push({ start, end, diagnostic });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we have the diagnostics subdivided per line, it's time to determine the _boundaries_
|
|
|
|
// where diagnostics need to appear.
|
|
|
|
// Later we will turn those boundaries into spans, and assign them appropriate classes.
|
|
|
|
|
|
|
|
let segmentedLines = new Map();
|
|
|
|
for (let [line, spans] of rawLineSpans) {
|
|
|
|
let lineBounds = lineMap.get(line);
|
|
|
|
|
|
|
|
let spanBorderSet = new Set([0]);
|
|
|
|
for (let { start, end } of spans) {
|
|
|
|
spanBorderSet.add(start);
|
|
|
|
spanBorderSet.add(end);
|
|
|
|
}
|
|
|
|
spanBorderSet.add(lineBounds.end - lineBounds.start);
|
|
|
|
let spanBorders = Array.from(spanBorderSet).sort((a, b) => a - b);
|
|
|
|
|
|
|
|
let segments = [];
|
|
|
|
let previous = 0;
|
|
|
|
for (let i = 1; i < spanBorders.length; ++i) {
|
|
|
|
segments.push({ start: previous, end: spanBorders[i], diagnostics: [] });
|
|
|
|
previous = spanBorders[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let span of spans) {
|
|
|
|
for (let segment of segments) {
|
|
|
|
if (segment.start >= span.start && segment.end <= span.end) {
|
|
|
|
segment.diagnostics.push(span.diagnostic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
segmentedLines.set(line, segments);
|
|
|
|
}
|
|
|
|
|
|
|
|
return segmentedLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
#renderErrorSquiggles(lines, element) {
|
|
|
|
if (this.errorSquiggles == null) return;
|
|
|
|
|
|
|
|
for (let i = 0; i < lines.lineBounds.length; ++i) {
|
|
|
|
let lineBounds = lines.lineBounds[i];
|
|
|
|
let lineElement = element.appendChild(document.createElement("span"));
|
|
|
|
lineElement.classList.add("line");
|
|
|
|
|
|
|
|
let segments = this.errorSquiggles.get(i);
|
|
|
|
if (segments != null) {
|
|
|
|
for (let segment of segments) {
|
|
|
|
let text = (lineBounds.substring + " ").substring(segment.start, segment.end);
|
|
|
|
if (segment.diagnostics.length == 0) {
|
|
|
|
lineElement.append(text);
|
|
|
|
} else {
|
|
|
|
let spanElement = lineElement.appendChild(document.createElement("span"));
|
|
|
|
spanElement.classList.add("squiggle", "squiggle-error");
|
|
|
|
spanElement.textContent = text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lineElement.textContent = lineBounds.substring;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-10 23:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("rkgk-brush-editor", BrushEditor);
|