mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-17 04:45:33 -08:00
Add the web console Notepad and shared skill tables, release v2.10.0
AI Assisted
This commit is contained in:
1 parent
124beb46db
commit
f8b54ef708
59 files changed
+12549
-321
No files matched your search
@@ -0,0 +1,201 @@
|
||||
// Plan2Code Web Console - the Notepad's save rules, status words and error sentences.
|
||||
//
|
||||
// The source of truth for when the page saves the note, how it retries, what
|
||||
// the status line says and the sentence each server refusal becomes. The
|
||||
// Notepad screen only runs timers and requests on what these return. No DOM
|
||||
// access at module scope and no imports: the page and `node --test` both
|
||||
// import this file. The limits mirror notepads.mjs (a test keeps them equal),
|
||||
// copied rather than imported because the page cannot load server modules.
|
||||
|
||||
export const SAVE_DEBOUNCE_MS = 800;
|
||||
export const RETRY_STEPS_MS = Object.freeze([1000, 2000, 4000, 8000, 16000, 30000]);
|
||||
export const NOTE_MAX_BYTES = 1024 * 1024;
|
||||
export const NOTEPAD_MAX_IMAGES = 200;
|
||||
export const IMAGE_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
|
||||
// The codes the /notepad routes send as `{ error: code }`.
|
||||
export const ERROR_CODES = Object.freeze(["bad", "too-long", "not-jpeg", "too-large", "too-many", "not-found"]);
|
||||
|
||||
// UTF-8 bytes, the unit the server's cap is in (a character count would let
|
||||
// non-ASCII text past the page's check and into a 413).
|
||||
export function noteBytes(text) {
|
||||
return new TextEncoder().encode(typeof text === "string" ? text : "").length;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ saving */
|
||||
|
||||
const STATUSES = Object.freeze(["idle", "pending", "saving", "saved", "failed", "too-long"]);
|
||||
|
||||
export const SAVE_START = Object.freeze({ status: "idle", attempt: 0 });
|
||||
|
||||
const known = (state) => !!state && typeof state === "object" && STATUSES.includes(state.status);
|
||||
const attemptOf = (state) => (Number.isInteger(state.attempt) && state.attempt > 0 ? state.attempt : 0);
|
||||
const stay = (state) => ({ state, timer: null });
|
||||
const to = (status, attempt, timer) => ({ state: { status, attempt }, timer });
|
||||
|
||||
/**
|
||||
* The save state after `event`, plus `timer`: the delay in ms the screen arms
|
||||
* before dispatching `due`, or null for no timer. Pure: never mutates `state`,
|
||||
* runs no clock and sends nothing.
|
||||
*/
|
||||
export function nextSave(state, event) {
|
||||
if (!known(state)) return stay(state);
|
||||
const type = event && typeof event === "object" ? event.type : event;
|
||||
const { status } = state;
|
||||
switch (type) {
|
||||
case "typed": {
|
||||
const bytes = event && typeof event.bytes === "number" ? event.bytes : 0;
|
||||
// Over the cap nothing is sent: the server would only refuse it.
|
||||
if (bytes > NOTE_MAX_BYTES) return to("too-long", 0, null);
|
||||
return to("pending", 0, SAVE_DEBOUNCE_MS);
|
||||
}
|
||||
case "due":
|
||||
if (status === "pending" || status === "failed") return to("saving", attemptOf(state), null);
|
||||
return stay(state);
|
||||
case "sent":
|
||||
return stay(state);
|
||||
case "ok":
|
||||
// A change typed while the request was out comes back as a fresh
|
||||
// `typed` from the screen, so `ok` can always land on saved.
|
||||
return to("saved", 0, null);
|
||||
case "error": {
|
||||
// Retrying a size the server refused would never succeed.
|
||||
if (event && event.code === "too-long") return to("too-long", 0, null);
|
||||
const attempt = attemptOf(state);
|
||||
return to("failed", attempt + 1, RETRY_STEPS_MS[Math.min(attempt, RETRY_STEPS_MS.length - 1)]);
|
||||
}
|
||||
case "retry":
|
||||
if (status === "failed") return to("saving", attemptOf(state), null);
|
||||
return stay(state);
|
||||
default:
|
||||
return stay(state);
|
||||
}
|
||||
}
|
||||
|
||||
// "1 MB", "512 KB", "300 bytes": the cap in words for the status line.
|
||||
function sizeWords(bytes) {
|
||||
const trim = (n) => String(Math.round(n * 10) / 10);
|
||||
if (bytes >= 1024 * 1024) return trim(bytes / (1024 * 1024)) + " MB";
|
||||
if (bytes >= 1024) return trim(bytes / 1024) + " KB";
|
||||
return bytes + " bytes";
|
||||
}
|
||||
|
||||
const STATUS_WORDS = Object.freeze({
|
||||
idle: "",
|
||||
pending: "Saving",
|
||||
saving: "Saving",
|
||||
saved: "Saved",
|
||||
failed: "Not saved (retrying)",
|
||||
"too-long": `Too long to save (over ${sizeWords(NOTE_MAX_BYTES)})`,
|
||||
});
|
||||
|
||||
const statusOf = (state) => (typeof state === "string" ? state : state && typeof state === "object" ? state.status : undefined);
|
||||
|
||||
// The status line's words for a state object or a bare status.
|
||||
export function saveStatus(state) {
|
||||
const status = statusOf(state);
|
||||
return Object.prototype.hasOwnProperty.call(STATUS_WORDS, status) ? STATUS_WORDS[status] : "";
|
||||
}
|
||||
|
||||
// True while text on screen may not be on disk yet: the page only asks
|
||||
// before closing then. A too-long note is not asked about, the status line
|
||||
// already says it cannot be saved.
|
||||
export function unsaved(state) {
|
||||
const status = statusOf(state);
|
||||
return status === "pending" || status === "saving" || status === "failed";
|
||||
}
|
||||
|
||||
// True while the editor holds text the stored note must not replace when the
|
||||
// notepad is opened again: anything unsaved, and a note too long to save,
|
||||
// which would otherwise lose everything typed since its last save.
|
||||
export function keepsText(state) {
|
||||
return unsaved(state) || statusOf(state) === "too-long";
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ errors */
|
||||
|
||||
const ERROR_SENTENCES = Object.freeze({
|
||||
bad: "Plan2Code could not read that request, so nothing was changed.",
|
||||
"too-long": `This note is too long to save. Notes can be up to ${sizeWords(NOTE_MAX_BYTES)}.`,
|
||||
"not-jpeg": "That picture could not be added. Try a PNG, JPEG or a screenshot.",
|
||||
"too-large": "That picture is too large to add. Try a smaller one.",
|
||||
"too-many": `This notepad already holds ${NOTEPAD_MAX_IMAGES} pictures, the most it can keep. Delete one to add another.`,
|
||||
"not-found": "That picture is no longer in this notepad.",
|
||||
});
|
||||
|
||||
const LOST_CONTACT = "Lost contact with Plan2Code. Your text is kept here and will be saved once it's back.";
|
||||
|
||||
// The status line when the note could not be read on first open: the editor
|
||||
// stays read-only, since anything typed would be saved over the real note.
|
||||
export const NOT_LOADED = "Could not read the note. Close the notepad and open it again.";
|
||||
|
||||
// One plain sentence for a server refusal; anything else (status 0, an
|
||||
// unknown code) is treated as a lost connection.
|
||||
export function errorSentence(code) {
|
||||
return typeof code === "string" && Object.prototype.hasOwnProperty.call(ERROR_SENTENCES, code) ? ERROR_SENTENCES[code] : LOST_CONTACT;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ editor */
|
||||
|
||||
// OverType's HTML without its inline style attributes, which the page's
|
||||
// policy refuses. A link's `anchor-name` (what OverType's link tooltip finds
|
||||
// the link by) moves to `data-anchor`; any other style attribute is dropped,
|
||||
// and the tooltip's own look comes from notepad.css instead. Typed text never
|
||||
// matches: OverType escapes the quotes in it.
|
||||
const STYLE_ATTR = / style="([^"]*)"/g;
|
||||
const ANCHOR_NAME = /^anchor-name: (--link-\d+)$/;
|
||||
|
||||
export function withoutStyleAttrs(html) {
|
||||
return html.replace(STYLE_ATTR, (_, css) => {
|
||||
const anchor = ANCHOR_NAME.exec(css);
|
||||
return anchor ? ` data-anchor="${anchor[1]}"` : "";
|
||||
});
|
||||
}
|
||||
|
||||
// OverType's HTML cut into its top-level elements (one per line, or one per
|
||||
// run of list items), as strings, so the page can tell which lines changed
|
||||
// without parsing the whole note into elements on every key. Tags are
|
||||
// counted, not parsed: OverType escapes `<` and `>` in text and attribute
|
||||
// values, so every `<` here starts a real tag. null when the HTML does not
|
||||
// end balanced, so the caller redraws everything instead.
|
||||
const TAG = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)\b[^>]*?(\/?)>/g;
|
||||
const VOID_TAGS = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr"]);
|
||||
|
||||
export function topLevelParts(html) {
|
||||
const parts = [];
|
||||
let depth = 0;
|
||||
let start = 0;
|
||||
TAG.lastIndex = 0;
|
||||
for (let m; (m = TAG.exec(html)); ) {
|
||||
if (depth === 0 && m.index > start) return null;
|
||||
const [, closing, name, selfClosing] = m;
|
||||
if (closing) depth -= 1;
|
||||
else if (!selfClosing && !VOID_TAGS.has(name.toLowerCase())) depth += 1;
|
||||
if (depth < 0) return null;
|
||||
if (depth === 0) {
|
||||
parts.push(html.slice(start, TAG.lastIndex));
|
||||
start = TAG.lastIndex;
|
||||
}
|
||||
}
|
||||
return depth === 0 && start === html.length ? parts : null;
|
||||
}
|
||||
|
||||
// How many parts at the start (`head`) and at the end (`tail`) are the same
|
||||
// in both lists, never overlapping: everything between them changed.
|
||||
export function changedRange(before, after) {
|
||||
const most = Math.min(before.length, after.length);
|
||||
let head = 0;
|
||||
while (head < most && before[head] === after[head]) head++;
|
||||
let tail = 0;
|
||||
while (tail < most - head && before[before.length - 1 - tail] === after[after.length - 1 - tail]) tail++;
|
||||
return { head, tail };
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ viewer */
|
||||
|
||||
// The next index when stepping through `count` pictures, wrapping at both
|
||||
// ends; -1 when there is nothing to step through.
|
||||
export function step(index, delta, count) {
|
||||
if (!(count > 0)) return -1;
|
||||
return (((index + delta) % count) + count) % count;
|
||||
}
|
||||
Reference in new issue
Block a user