@vedivad/typst-web-service 0.8.1 → 0.9.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
@@ -27,6 +27,8 @@ interface LspCompletionItem {
27
27
  detail?: string;
28
28
  documentation?: string | LspMarkupContent;
29
29
  insertText?: string;
30
+ /** LSP InsertTextFormat: 1 = PlainText, 2 = Snippet (TextMate syntax). */
31
+ insertTextFormat?: number;
30
32
  filterText?: string;
31
33
  sortText?: string;
32
34
  textEdit?: {
@@ -75,18 +77,24 @@ declare class TypstAnalyzer {
75
77
  private readonly proxy;
76
78
  private readonly worker;
77
79
  private versionCounter;
78
- private openedUris;
80
+ /**
81
+ * Last content pushed to the worker per URI. Presence is the source of
82
+ * truth for "is this URI opened on the worker?"; value drives own-RPC dedup.
83
+ */
84
+ private readonly content;
79
85
  private constructor();
80
86
  static create(options: TypstAnalyzerOptions): Promise<TypstAnalyzer>;
81
87
  didOpen(uri: string, content: string): Promise<void>;
82
88
  didClose(uri: string): Promise<void>;
83
89
  /**
84
- * Notify the analyzer that a document has changed.
90
+ * Notify the analyzer that a document has changed. Skips the RPC when the
91
+ * content matches what the worker last saw.
85
92
  */
86
93
  didChange(uri: string, content: string): Promise<void>;
87
94
  /**
88
95
  * Batch document changes. Splits inputs into opens (first-time URIs) and
89
96
  * changes (already-open URIs) and sends them in a single worker roundtrip.
97
+ * Skips unchanged documents; returns without an RPC if nothing is pending.
90
98
  */
91
99
  didChangeMany(docs: Record<string, string>): Promise<void>;
92
100
  /**
@@ -94,8 +102,20 @@ declare class TypstAnalyzer {
94
102
  * in a single worker roundtrip.
95
103
  */
96
104
  didCloseMany(uris: string[]): Promise<void>;
97
- completion(uri: string, line: number, character: number): Promise<LspCompletionResponse>;
98
- hover(uri: string, line: number, character: number): Promise<LspHover | null>;
105
+ /**
106
+ * Request completion at `position` for `uri`, with `content` as the current
107
+ * document state. Bundles the didChange notification with the request in one
108
+ * worker roundtrip; degrades to a plain completion request when the worker
109
+ * already has this exact content.
110
+ */
111
+ completion(uri: string, content: string, position: LspPosition): Promise<LspCompletionResponse>;
112
+ /**
113
+ * Request hover at `position` for `uri`, with `content` as the current
114
+ * document state. Bundles the didChange notification with the request in one
115
+ * worker roundtrip; degrades to a plain hover request when the worker
116
+ * already has this exact content.
117
+ */
118
+ hover(uri: string, content: string, position: LspPosition): Promise<LspHover | null>;
99
119
  destroy(): void;
100
120
  }
101
121
 
@@ -163,7 +183,7 @@ interface TypstCompilerOptions {
163
183
  /**
164
184
  * Explicit Worker instance. When omitted, an inlined blob worker is created automatically.
165
185
  * Use this for Vite apps to get proper source maps:
166
- * `await TypstCompiler.create({ worker: new Worker(new URL('typst-web-service/worker', import.meta.url)) })`
186
+ * `await TypstCompiler.create({ worker: new Worker(new URL('typst-web-service/compiler-worker', import.meta.url)) })`
167
187
  */
168
188
  worker?: Worker;
169
189
  /**
@@ -194,6 +214,8 @@ declare class TypstCompiler {
194
214
  private readonly proxy;
195
215
  private readonly worker;
196
216
  private readonly encoder;
217
+ /** Last text content pushed per path. Drives own-RPC dedup. Invalidated by binary writes. */
218
+ private readonly content;
197
219
  private constructor();
198
220
  static create(options?: TypstCompilerOptions): Promise<TypstCompiler>;
199
221
  /**
@@ -208,14 +230,18 @@ declare class TypstCompiler {
208
230
  * override with `entry`.
209
231
  */
210
232
  compilePdf(entry?: string): Promise<Uint8Array>;
211
- /** Add or overwrite a text file in the virtual compiler filesystem. */
233
+ /**
234
+ * Add or overwrite a text file in the virtual compiler filesystem. Skips
235
+ * the worker RPC when `source` matches the last value pushed for `path`.
236
+ */
212
237
  setText(path: string, source: string): Promise<void>;
213
238
  /** Add or overwrite a JSON file in the virtual compiler filesystem. */
214
239
  setJson(path: string, value: unknown, replacer?: (this: unknown, key: string, value: unknown) => unknown, space?: string | number): Promise<void>;
215
240
  /**
216
241
  * Add or overwrite multiple files in the virtual compiler filesystem in a
217
- * single worker roundtrip. Strings are UTF-8 encoded; Uint8Arrays are passed
218
- * through.
242
+ * single worker roundtrip. Strings are UTF-8 encoded; Uint8Arrays are
243
+ * passed through. Text entries unchanged since their last push are skipped;
244
+ * binary entries always push and invalidate the text cache for their path.
219
245
  */
220
246
  setMany(files: Record<string, string | Uint8Array>): Promise<void>;
221
247
  /** Add or overwrite a binary file in the virtual compiler filesystem. */
@@ -285,6 +311,18 @@ interface TypstProjectOptions {
285
311
  * and project VFS use the raw paths unchanged.
286
312
  */
287
313
  analyzerUriRoot?: string;
314
+ /**
315
+ * Idle time (ms) after the last VFS mutation before an auto-compile fires.
316
+ * Default: 0 — compile fires on the next macrotask. Set higher (e.g. 150) to
317
+ * coalesce rapid edits.
318
+ */
319
+ compileDebounceMs?: number;
320
+ /**
321
+ * Maximum time (ms) between auto-compiles during sustained mutation bursts.
322
+ * Guarantees progress while the user is actively typing. Default: 0 (no
323
+ * throttle — only debounce applies).
324
+ */
325
+ compileThrottleMs?: number;
288
326
  }
289
327
  /**
290
328
  * Coordinates a compiler + analyzer pair for multi-file Typst projects.
@@ -303,15 +341,27 @@ declare class TypstProject {
303
341
  private readonly compiler;
304
342
  private readonly analyzer?;
305
343
  private readonly analyzerUriRoot;
306
- private readonly trackedTextPaths;
307
- /** Last content written via setText/setMany, per path. Used to skip redundant writes to compiler + analyzer. */
308
- private readonly lastSyncedContent;
344
+ /**
345
+ * Tracked text files: path latest content observed. Presence in this map
346
+ * is the source of truth for "is this a tracked text file?"; insertion
347
+ * order drives the `files` getter. Per-sink dedup lives in the compiler and
348
+ * analyzer.
349
+ */
350
+ private readonly contentByPath;
309
351
  private readonly compileListeners;
352
+ private readonly scheduler;
310
353
  private compileVersion;
311
354
  private _lastResult;
312
355
  private _entry;
313
356
  private destroyed;
314
357
  constructor(options: TypstProjectOptions);
358
+ /**
359
+ * Schedule an auto-compile after VFS mutations. Coalesces rapid calls via
360
+ * the configured debounce/throttle. Errors surface through `onCompile`
361
+ * listeners via a synthetic diagnostic; callers awaiting a specific compile
362
+ * should call `compile()` directly.
363
+ */
364
+ private scheduleCompile;
315
365
  /** Current entry file path. Assign to change the sticky entry used by subsequent `compile()` calls. */
316
366
  get entry(): Path;
317
367
  set entry(path: Path);
@@ -337,8 +387,9 @@ declare class TypstProject {
337
387
  getText(path: Path): string | undefined;
338
388
  /**
339
389
  * Add or overwrite a text file. Goes to the compiler's VFS and, when an
340
- * analyzer is attached, to the analyzer as a document change. Redundant
341
- * calls with unchanged content are skipped.
390
+ * analyzer is attached, to the analyzer as a document change. No-op when
391
+ * the tracked path already has this exact content skips both worker RPCs
392
+ * and the auto-scheduled compile.
342
393
  */
343
394
  setText(path: Path, content: string): Promise<void>;
344
395
  /**
@@ -350,7 +401,9 @@ declare class TypstProject {
350
401
  setBinary(path: Path, content: ArrayBuffer | ArrayBufferView): Promise<void>;
351
402
  /**
352
403
  * Batch set multiple files. Strings route to both compiler and analyzer;
353
- * Uint8Array entries go to the compiler only.
404
+ * Uint8Array entries go to the compiler only. Strings matching the last
405
+ * tracked content for their path are skipped on both sinks. Binary entries
406
+ * always go through (no content cache, so no dedup).
354
407
  */
355
408
  setMany(files: Record<Path, string | Uint8Array>): Promise<void>;
356
409
  /**
@@ -374,15 +427,28 @@ declare class TypstProject {
374
427
  * callers and listeners always receive a `CompileResult`. Listeners are
375
428
  * notified only for the most recent compile — results from an earlier call
376
429
  * that resolves after a later one are suppressed.
430
+ *
431
+ * VFS mutations (`setText`, `remove`, etc.) auto-schedule a debounced
432
+ * compile; call this directly only when you need an awaitable handle on the
433
+ * result (e.g., to flush before rendering to PDF).
377
434
  */
378
435
  compile(): Promise<CompileResult>;
379
436
  /** Compile the current VFS state to PDF using the sticky entry. */
380
437
  compilePdf(): Promise<Uint8Array>;
381
438
  private requireAnalyzer;
382
- /** Request completions at the given position. Throws when no analyzer is attached. */
383
- completion(path: Path, line: number, character: number): Promise<LspCompletionResponse>;
384
- /** Request hover info at the given position. Throws when no analyzer is attached. */
385
- hover(path: Path, line: number, character: number): Promise<LspHover | null>;
439
+ /**
440
+ * Request completion for `path` at `position`, using `source` as the
441
+ * current document state. One analyzer roundtrip; compiler is not touched —
442
+ * the compile sync path writes to the compiler separately. Throws when no
443
+ * analyzer is attached.
444
+ */
445
+ completion(path: Path, source: string, position: LspPosition): Promise<LspCompletionResponse>;
446
+ /**
447
+ * Request hover for `path` at `position`, using `source` as the current
448
+ * document state. One analyzer roundtrip; compiler is not touched. Throws
449
+ * when no analyzer is attached.
450
+ */
451
+ hover(path: Path, source: string, position: LspPosition): Promise<LspHover | null>;
386
452
  /**
387
453
  * Tear down the project and the services it owns. Destroys the attached
388
454
  * compiler and analyzer, drops all listeners, and clears VFS tracking state.
@@ -400,6 +466,16 @@ interface TypstRendererOptions {
400
466
  /** URL to the typst-ts-renderer WASM binary. Defaults to jsDelivr CDN. */
401
467
  wasmUrl?: string;
402
468
  }
469
+ interface RenderedSvgPage {
470
+ /** Zero-based page index within the document. */
471
+ index: number;
472
+ /** Page width in typographic points. */
473
+ width: number;
474
+ /** Page height in typographic points. */
475
+ height: number;
476
+ /** Standalone SVG string for just this page. */
477
+ svg: string;
478
+ }
403
479
  /**
404
480
  * Converts Typst vector artifacts to SVG strings.
405
481
  *
@@ -419,6 +495,15 @@ declare class TypstRenderer {
419
495
  destroy(): Promise<void>;
420
496
  /** Render a Typst vector artifact to an SVG string. */
421
497
  renderSvg(vector: Uint8Array): Promise<string>;
498
+ /**
499
+ * Render a Typst vector artifact into one self-contained SVG string per
500
+ * physical page. The merged SVG is split by `<g class="typst-page">`
501
+ * children; each group's `data-page-width` / `data-page-height` give the
502
+ * page-local viewBox. Shared `<defs>` / `<style>` are duplicated into each
503
+ * page so the output SVGs render independently. Returns an empty array if
504
+ * the document has no page groups.
505
+ */
506
+ renderSvgPages(vector: Uint8Array): Promise<RenderedSvgPage[]>;
422
507
  }
423
508
 
424
- export { type AnalyzerUri, type CompileResult, type DiagnosticMessage, type DiagnosticRange, type FormatConfig, type FormatRangeResult, type LspCompletionItem, type LspCompletionList, type LspCompletionResponse, type LspDiagnostic, type LspHover, type LspHoverContents, type LspMarkupContent, type LspPosition, type LspRange, type Path, TypstAnalyzer, type TypstAnalyzerOptions, TypstCompiler, type TypstCompilerOptions, TypstFormatter, TypstProject, type TypstProjectOptions, TypstRenderer, normalizePath, normalizeRoot, pathToAnalyzerUri };
509
+ export { type AnalyzerUri, type CompileResult, type DiagnosticMessage, type DiagnosticRange, type FormatConfig, type FormatRangeResult, type LspCompletionItem, type LspCompletionList, type LspCompletionResponse, type LspDiagnostic, type LspHover, type LspHoverContents, type LspMarkupContent, type LspPosition, type LspRange, type Path, type RenderedSvgPage, TypstAnalyzer, type TypstAnalyzerOptions, TypstCompiler, type TypstCompilerOptions, TypstFormatter, TypstProject, type TypstProjectOptions, TypstRenderer, type TypstRendererOptions, normalizePath, normalizeRoot, pathToAnalyzerUri };