@vedivad/typst-web-service 0.8.1 → 0.9.1

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,25 @@ interface TypstProjectOptions {
285
311
  * and project VFS use the raw paths unchanged.
286
312
  */
287
313
  analyzerUriRoot?: string;
314
+ /**
315
+ * Scheduling for auto-compiles after VFS mutations. Mutations debounce by
316
+ * `debounceMs`; `maxWaitMs` caps how long the debounce can keep deferring
317
+ * during sustained edits so the user still sees progress.
318
+ */
319
+ autoCompile?: AutoCompileOptions;
320
+ }
321
+ interface AutoCompileOptions {
322
+ /**
323
+ * Idle time (ms) after the last VFS mutation before a compile fires.
324
+ * Default: 0 — compile fires on the next macrotask. Set higher (e.g. 150) to
325
+ * coalesce rapid edits.
326
+ */
327
+ debounceMs?: number;
328
+ /**
329
+ * Maximum time (ms) the debounce is allowed to defer a compile during
330
+ * sustained mutation bursts. Default: 0 (no cap — pure debounce).
331
+ */
332
+ maxWaitMs?: number;
288
333
  }
289
334
  /**
290
335
  * Coordinates a compiler + analyzer pair for multi-file Typst projects.
@@ -303,15 +348,28 @@ declare class TypstProject {
303
348
  private readonly compiler;
304
349
  private readonly analyzer?;
305
350
  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;
351
+ /**
352
+ * Tracked text files: path latest content observed. Presence in this map
353
+ * is the source of truth for "is this a tracked text file?"; insertion
354
+ * order drives the `files` getter. Per-sink dedup lives in the compiler and
355
+ * analyzer.
356
+ */
357
+ private readonly contentByPath;
309
358
  private readonly compileListeners;
359
+ private readonly scheduler;
310
360
  private compileVersion;
311
361
  private _lastResult;
312
362
  private _entry;
313
363
  private destroyed;
364
+ private invokeListener;
314
365
  constructor(options: TypstProjectOptions);
366
+ /**
367
+ * Schedule an auto-compile after VFS mutations. Coalesces rapid calls via
368
+ * the configured debounce/throttle. Errors surface through `onCompile`
369
+ * listeners via a synthetic diagnostic; callers awaiting a specific compile
370
+ * should call `compile()` directly.
371
+ */
372
+ private scheduleCompile;
315
373
  /** Current entry file path. Assign to change the sticky entry used by subsequent `compile()` calls. */
316
374
  get entry(): Path;
317
375
  set entry(path: Path);
@@ -337,8 +395,9 @@ declare class TypstProject {
337
395
  getText(path: Path): string | undefined;
338
396
  /**
339
397
  * 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.
398
+ * analyzer is attached, to the analyzer as a document change. No-op when
399
+ * the tracked path already has this exact content skips both worker RPCs
400
+ * and the auto-scheduled compile.
342
401
  */
343
402
  setText(path: Path, content: string): Promise<void>;
344
403
  /**
@@ -350,7 +409,9 @@ declare class TypstProject {
350
409
  setBinary(path: Path, content: ArrayBuffer | ArrayBufferView): Promise<void>;
351
410
  /**
352
411
  * Batch set multiple files. Strings route to both compiler and analyzer;
353
- * Uint8Array entries go to the compiler only.
412
+ * Uint8Array entries go to the compiler only. Strings matching the last
413
+ * tracked content for their path are skipped on both sinks. Binary entries
414
+ * always go through (no content cache, so no dedup).
354
415
  */
355
416
  setMany(files: Record<Path, string | Uint8Array>): Promise<void>;
356
417
  /**
@@ -374,15 +435,28 @@ declare class TypstProject {
374
435
  * callers and listeners always receive a `CompileResult`. Listeners are
375
436
  * notified only for the most recent compile — results from an earlier call
376
437
  * that resolves after a later one are suppressed.
438
+ *
439
+ * VFS mutations (`setText`, `remove`, etc.) auto-schedule a debounced
440
+ * compile; call this directly only when you need an awaitable handle on the
441
+ * result (e.g., to flush before rendering to PDF).
377
442
  */
378
443
  compile(): Promise<CompileResult>;
379
444
  /** Compile the current VFS state to PDF using the sticky entry. */
380
445
  compilePdf(): Promise<Uint8Array>;
381
446
  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>;
447
+ /**
448
+ * Request completion for `path` at `position`, using `source` as the
449
+ * current document state. One analyzer roundtrip; compiler is not touched —
450
+ * the compile sync path writes to the compiler separately. Throws when no
451
+ * analyzer is attached.
452
+ */
453
+ completion(path: Path, source: string, position: LspPosition): Promise<LspCompletionResponse>;
454
+ /**
455
+ * Request hover for `path` at `position`, using `source` as the current
456
+ * document state. One analyzer roundtrip; compiler is not touched. Throws
457
+ * when no analyzer is attached.
458
+ */
459
+ hover(path: Path, source: string, position: LspPosition): Promise<LspHover | null>;
386
460
  /**
387
461
  * Tear down the project and the services it owns. Destroys the attached
388
462
  * compiler and analyzer, drops all listeners, and clears VFS tracking state.
@@ -400,6 +474,16 @@ interface TypstRendererOptions {
400
474
  /** URL to the typst-ts-renderer WASM binary. Defaults to jsDelivr CDN. */
401
475
  wasmUrl?: string;
402
476
  }
477
+ interface RenderedSvgPage {
478
+ /** Zero-based page index within the document. */
479
+ index: number;
480
+ /** Page width in typographic points. */
481
+ width: number;
482
+ /** Page height in typographic points. */
483
+ height: number;
484
+ /** Standalone SVG string for just this page. */
485
+ svg: string;
486
+ }
403
487
  /**
404
488
  * Converts Typst vector artifacts to SVG strings.
405
489
  *
@@ -419,6 +503,15 @@ declare class TypstRenderer {
419
503
  destroy(): Promise<void>;
420
504
  /** Render a Typst vector artifact to an SVG string. */
421
505
  renderSvg(vector: Uint8Array): Promise<string>;
506
+ /**
507
+ * Render a Typst vector artifact into one self-contained SVG string per
508
+ * physical page. The merged SVG is split by `<g class="typst-page">`
509
+ * children; each group's `data-page-width` / `data-page-height` give the
510
+ * page-local viewBox. Shared `<defs>` / `<style>` are duplicated into each
511
+ * page so the output SVGs render independently. Returns an empty array if
512
+ * the document has no page groups.
513
+ */
514
+ renderSvgPages(vector: Uint8Array): Promise<RenderedSvgPage[]>;
422
515
  }
423
516
 
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 };
517
+ 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 };