tsquare 0.10.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export { printWireframeText as formatWireframe } from "./print.js";
5
5
  export { encodeWireframe, decodeWireframe, MAX_SHARED_TEXT } from "./share.js";
6
6
  export { mcpTools, MCP_INSTRUCTIONS, LINK_KINDS, DEFAULT_LINKS, type LinkKind, wireframeGuide, renderWireframeTool, shareWireframeTool, callMcpTool, type ToolResult, type ToolContent, type ToolOptions, type McpToolName } from "./mcp.js";
7
7
  export { upgradeWireframe, upgradeSpec, RENAMED_PROPS, type UpgradeOptions } from "./upgrade.js";
8
+ export { renderAll, DEFAULT_SELECTOR, type RenderAllOptions, type RenderAllResult } from "./dom.js";
8
9
  export { renderWireframeSvg, renderWireframePng, checkSpec, boardSize, boardLayout, type BoardItem, type RenderWireframeOptions } from "./render.js";
9
10
  export { parseWireframeText, type TextIssue, type ParseResult } from "./text.js";
10
11
  export { catalog, componentDefinitions } from "./catalog.js";
package/dist/index.js CHANGED
@@ -166,8 +166,83 @@ async function callMcpTool(name, args = {}, opts = {}) {
166
166
  if (!parsed.success) return { content: [text(`Invalid arguments: ${z.prettifyError(parsed.error)}`)], isError: true };
167
167
  return name === "render_wireframe" ? renderWireframeTool(parsed.data) : shareWireframeTool(parsed.data, opts);
168
168
  }
169
+
170
+ // src/dom.ts
171
+ var DEFAULT_SELECTOR = ".tsquare, .language-tsquare, [data-lang='tsquare']";
172
+ var DONE = "data-tsquare";
173
+ var STYLES = `:where(.tsquare-board){margin:1em 0}
174
+ :where(.tsquare-board) img{display:block;max-width:100%;height:auto}
175
+ :where(.tsquare-problems){margin:1em 0;padding:.5em .75em;border-left:3px solid #d33;font-size:.9em}
176
+ :where(.tsquare-problems) ul{margin:.25em 0 0;padding-left:1.25em;font-family:ui-monospace,Menlo,monospace}`;
177
+ async function renderAll(opts = {}) {
178
+ const root = opts.root ?? document;
179
+ const doc = root.ownerDocument ?? root;
180
+ const result = { rendered: 0, invalid: 0 };
181
+ const blocks = [];
182
+ for (const el of root.querySelectorAll(opts.selector ?? DEFAULT_SELECTOR)) {
183
+ const target = el.tagName === "CODE" ? el.closest("pre") ?? el : el;
184
+ if (target.hasAttribute(DONE) || target.closest(`[${DONE}]`)) continue;
185
+ if (blocks.some((b) => b.target.contains(target) || target.contains(b.target))) continue;
186
+ blocks.push({ target, text: el.tagName === "CODE" ? el : el.querySelector("code") ?? el.querySelector("pre") ?? el });
187
+ }
188
+ if (blocks.length) addStyles(doc);
189
+ for (const { target, text: text2 } of blocks) {
190
+ const source = text2.textContent ?? "";
191
+ const replacement = await renderBlock(doc, source);
192
+ replacement.setAttribute(DONE, "");
193
+ target.replaceWith(replacement);
194
+ if (replacement.classList.contains("tsquare-board")) result.rendered++;
195
+ else result.invalid++;
196
+ }
197
+ return result;
198
+ }
199
+ async function renderBlock(doc, source) {
200
+ const { spec, issues } = compileWireframe(source);
201
+ if (!spec || issues.length) return problems2(doc, issues.map((i) => `line ${i.line}: ${i.message}`));
202
+ try {
203
+ const svg = await renderWireframeSvg(spec, { skipValidation: true });
204
+ const { width, height } = boardLayout(spec);
205
+ const title = spec.elements[spec.root]?.props?.title;
206
+ const figure = doc.createElement("figure");
207
+ figure.className = "tsquare-board";
208
+ const img = doc.createElement("img");
209
+ img.src = svgDataUrl(svg);
210
+ img.width = width;
211
+ img.height = height;
212
+ img.alt = title ? `Wireframe: ${title}` : "Wireframe";
213
+ figure.append(img);
214
+ return figure;
215
+ } catch (err) {
216
+ return problems2(doc, [`tsquare couldn't render this board: ${err instanceof Error ? err.message : String(err)}`]);
217
+ }
218
+ }
219
+ function svgDataUrl(svg) {
220
+ const bytes = new TextEncoder().encode(svg);
221
+ let binary = "";
222
+ for (let i = 0; i < bytes.length; i += 32768) binary += String.fromCharCode(...bytes.subarray(i, i + 32768));
223
+ return `data:image/svg+xml;base64,${btoa(binary)}`;
224
+ }
225
+ function problems2(doc, lines) {
226
+ const box = doc.createElement("div");
227
+ box.className = "tsquare-problems";
228
+ box.setAttribute("role", "note");
229
+ const title = doc.createElement("strong");
230
+ title.textContent = lines.length === 1 ? "1 problem in this wireframe" : `${lines.length} problems in this wireframe`;
231
+ const list = doc.createElement("ul");
232
+ for (const line of lines) list.append(Object.assign(doc.createElement("li"), { textContent: line }));
233
+ box.append(title, list);
234
+ return box;
235
+ }
236
+ function addStyles(doc) {
237
+ if (doc.getElementById("tsquare-styles")) return;
238
+ const style = doc.createElement("style");
239
+ style.id = "tsquare-styles";
240
+ style.textContent = STYLES;
241
+ doc.head.append(style);
242
+ }
169
243
  export {
170
244
  DEFAULT_LINKS,
245
+ DEFAULT_SELECTOR,
171
246
  DEVICES,
172
247
  LINK_KINDS,
173
248
  MAX_SHARED_TEXT,
@@ -189,6 +264,7 @@ export {
189
264
  mcpTools,
190
265
  parseWireframeText,
191
266
  registry,
267
+ renderAll,
192
268
  renderWireframe,
193
269
  renderWireframePng,
194
270
  renderWireframeSvg,
@@ -0,0 +1,13 @@
1
+ import type { LanguageData } from "./language-data.js";
2
+ export interface EditorOptions {
3
+ /** The starting text. */
4
+ doc?: string;
5
+ /** Autocomplete for components, props, option words, icons and accents: pass languageData(). Without it, no autocomplete. */
6
+ data?: LanguageData;
7
+ /** Called with the whole text after every change. */
8
+ onChange?: (text: string) => void;
9
+ /** Line numbers in a gutter. Default true. */
10
+ lineNumbers?: boolean;
11
+ /** The editable area's accessible name. Default "Wireframe text". */
12
+ ariaLabel?: string;
13
+ }
@@ -0,0 +1,18 @@
1
+ import { EditorView } from "@codemirror/view";
2
+ import type { EditorOptions } from "./editor-options.js";
3
+ export interface Editor {
4
+ view: EditorView;
5
+ getText(): string;
6
+ /** Replace the whole document as one undoable step. */
7
+ setText(text: string): void;
8
+ setIssues(issues: {
9
+ line: number;
10
+ message: string;
11
+ }[]): void;
12
+ goToLine(line: number): void;
13
+ /** Scroll the cursor back into view (after the panels around the editor change size). */
14
+ revealCursor(): void;
15
+ /** Remove the editor and its listeners. */
16
+ destroy(): void;
17
+ }
18
+ export declare function createEditor(parent: HTMLElement, opts?: EditorOptions): Editor;
@@ -75,6 +75,28 @@
75
75
  --syn-punct: #9aa4b2;
76
76
  color-scheme: dark;
77
77
  }
78
+ /* the editor (tsquare/editor) reads these: the playground's own colors, light or dark */
79
+ :root {
80
+ --tsquare-editor-bg: var(--editor-bg);
81
+ --tsquare-editor-fg: var(--editor-ink);
82
+ --tsquare-editor-gutter: var(--muted);
83
+ --tsquare-editor-active-line: var(--editor-active);
84
+ --tsquare-editor-selection: var(--editor-selection);
85
+ --tsquare-editor-error: var(--error);
86
+ --tsquare-editor-popup-bg: var(--panel);
87
+ --tsquare-editor-popup-border: var(--line);
88
+ --tsquare-editor-popup-selected: var(--accent-soft);
89
+ --tsquare-editor-font: var(--mono);
90
+ --tsquare-editor-ui-font: var(--sans);
91
+ --tsquare-syntax-component: var(--syn-component);
92
+ --tsquare-syntax-string: var(--syn-string);
93
+ --tsquare-syntax-prop: var(--syn-prop);
94
+ --tsquare-syntax-number: var(--syn-number);
95
+ --tsquare-syntax-word: var(--syn-word);
96
+ --tsquare-syntax-color: var(--syn-color);
97
+ --tsquare-syntax-comment: var(--syn-comment);
98
+ --tsquare-syntax-punct: var(--syn-punct);
99
+ }
78
100
  * { box-sizing: border-box; }
79
101
  html, body { height: 100%; margin: 0; }
80
102
  body { background: var(--bg); color: var(--ink); font: 14px/1.45 var(--sans); display: flex; flex-direction: column; }