@embedpdf/plugin-annotation 3.0.0-next.21 → 3.0.0-next.22

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.cjs CHANGED
@@ -1138,7 +1138,8 @@ function createChromeReads(ctx, { store, geometry, behaviors, settings }) {
1138
1138
  *
1139
1139
  * • a flat props patch → the run delta it means for a text range (font,
1140
1140
  * size, colour, bold/italic/underline) and the keys left for the body
1141
- * • what a range reads back for those keys (agree → the value, else mixed)
1141
+ * • what a range reads back for those keys (agree → the value, else mixed),
1142
+ * and a caret: the text typed there next
1142
1143
  * • faces: the DTO font a face names
1143
1144
  * • the commit rule: a document nothing overrides on a plain annotation
1144
1145
  * commits as `contents`; anything else as `richText`
@@ -1266,11 +1267,24 @@ function runsInRange(doc, range) {
1266
1267
  }
1267
1268
  return out;
1268
1269
  }
1269
- /** The values a range reads back for the range keys — resolved against the
1270
- * body, one value where every run agrees, `mixed` where they don't. */
1270
+ /** The values a range reads back for the range keys: the runs it covers (`runProps`). */
1271
1271
  function rangeProps(doc, range, fonts) {
1272
- const runs = runsInRange(doc, range);
1273
- const body = doc.body;
1272
+ return runProps(doc.body, runsInRange(doc, range), fonts);
1273
+ }
1274
+ /**
1275
+ * The values a caret reads back for the range keys: the text typed there
1276
+ * next, in the style of the text before it (`styleAt`) with the style chosen
1277
+ * at the caret over it.
1278
+ */
1279
+ function caretProps(doc, at, typing, fonts) {
1280
+ return runProps(doc.body, [{
1281
+ ...(0, _embedpdf_core_annotation.styleAt)(doc, at),
1282
+ ...typing
1283
+ }], fonts);
1284
+ }
1285
+ /** The values runs read back for the range keys: resolved against the
1286
+ * body, one value where every run agrees, `mixed` where they don't. */
1287
+ function runProps(body, runs, fonts) {
1274
1288
  const resolve = (delta) => ({
1275
1289
  family: delta.family ?? body.family,
1276
1290
  weight: delta.weight ?? body.weight,
@@ -1349,12 +1363,24 @@ function propertyValues(properties, sources) {
1349
1363
  * selection of frozen annotations has nothing to restyle.
1350
1364
  */
1351
1365
  function createPropertyReads(ctx, { store, fonts }) {
1366
+ /** The editor's selection inside the annotation being edited, or null (no
1367
+ * editor selection, or one left behind by a previous edit). */
1368
+ const editorSelection = (model) => {
1369
+ const ts = ctx.state.get().textSelection;
1370
+ return ts && model.editing === ts.id && model.byId[ts.id] ? ts : null;
1371
+ };
1352
1372
  /** The editor's text range inside the annotation being edited, or null
1353
- * (no editor selection, a bare caret, or a selection left behind by a
1354
- * previous edit). The one condition that routes the range properties to runs. */
1373
+ * (no editor selection, or a bare caret). The one condition that routes
1374
+ * the range properties to runs. */
1355
1375
  const activeTextRange = (model) => {
1356
- const ts = ctx.state.get().textSelection;
1357
- return ts && model.editing === ts.id && ts.end > ts.start && model.byId[ts.id] ? ts : null;
1376
+ const ts = editorSelection(model);
1377
+ return ts && ts.end > ts.start ? ts : null;
1378
+ };
1379
+ /** The editor's caret inside the annotation being edited, or null. The
1380
+ * range properties there are the text typed next. */
1381
+ const activeCaret = (model) => {
1382
+ const ts = editorSelection(model);
1383
+ return ts && ts.end === ts.start ? ts : null;
1358
1384
  };
1359
1385
  /** What a panel reads of one member: its fields, its text as shown, and its link. */
1360
1386
  const sourcesOf = (model, record) => {
@@ -1366,29 +1392,42 @@ function createPropertyReads(ctx, { store, fonts }) {
1366
1392
  link: annotation.subtype === "link" ? annotation.target ?? null : (0, _embedpdf_core_annotation.linkOf)(model, record.id)
1367
1393
  };
1368
1394
  };
1395
+ /**
1396
+ * What a free text's range properties read: the text the next change
1397
+ * styles. The editor's range, the text typed next at its caret, else all of
1398
+ * the box's text. `null` for any other kind.
1399
+ */
1400
+ const textPropsOf = (model, record) => {
1401
+ if (record.annotation.subtype !== "free-text") return null;
1402
+ const doc = (0, _embedpdf_core_annotation.richDocOf)(record.annotation, fonts);
1403
+ const ts = editorSelection(model);
1404
+ if (ts?.id !== record.id) return rangeProps(doc, {
1405
+ start: 0,
1406
+ end: (0, _embedpdf_core_annotation.richTextLength)(doc)
1407
+ }, fonts);
1408
+ if (ts.end > ts.start) return rangeProps(doc, ts, fonts);
1409
+ return caretProps(doc, ts.start, ts.typing, fonts);
1410
+ };
1369
1411
  let cache = null;
1370
1412
  const selectionPropertiesOf = () => {
1371
1413
  const model = store.model();
1372
- const range = activeTextRange(model);
1373
- if (cache && cache.model === model && cache.range === range) return cache.v;
1414
+ const selection = editorSelection(model);
1415
+ if (cache && cache.model === model && cache.selection === selection) return cache.v;
1374
1416
  const members = model.selected.map((id) => model.byId[id]).filter((record) => !!record && (0, _embedpdf_core_annotation.isEditableByHand)(model, record.annotation));
1375
- const properties = (0, _embedpdf_core_annotation.sharedProperties)(members.map((record) => (0, _embedpdf_core_annotation.kindOf)(record.annotation)));
1417
+ const properties = (0, _embedpdf_core_annotation.selectionProperties)(model, members.map((record) => record.annotation));
1376
1418
  const sources = members.map((record) => sourcesOf(model, record));
1419
+ const texts = members.map((record) => textPropsOf(model, record));
1420
+ const valueOf = (spec, index) => {
1421
+ const text = texts[index];
1422
+ return text && RANGE_KEYS.includes(spec.key) ? text.values[spec.key] : propertyValue(spec, sources[index]);
1423
+ };
1377
1424
  const values = sources.length ? propertyValues(properties, sources[0]) : {};
1378
1425
  const mixed = [];
1379
1426
  for (const spec of properties) {
1380
- const first = JSON.stringify(propertyValue(spec, sources[0]));
1381
- if (sources.some((source) => JSON.stringify(propertyValue(spec, source)) !== first)) mixed.push(spec.key);
1382
- }
1383
- if (range && members.length === 1 && members[0].id === range.id) {
1384
- const rp = rangeProps((0, _embedpdf_core_annotation.richDocOf)(members[0].annotation, fonts), range, fonts);
1385
- for (const spec of properties) {
1386
- if (!RANGE_KEYS.includes(spec.key)) continue;
1387
- values[spec.key] = rp.values[spec.key];
1388
- const index = mixed.indexOf(spec.key);
1389
- if (index >= 0) mixed.splice(index, 1);
1390
- if (rp.mixed.includes(spec.key)) mixed.push(spec.key);
1391
- }
1427
+ const ofText = RANGE_KEYS.includes(spec.key);
1428
+ if (ofText && texts[0]) values[spec.key] = texts[0].values[spec.key];
1429
+ const first = JSON.stringify(valueOf(spec, 0));
1430
+ if (sources.some((_, index) => JSON.stringify(valueOf(spec, index)) !== first) || ofText && texts.some((text) => text?.mixed.includes(spec.key))) mixed.push(spec.key);
1392
1431
  }
1393
1432
  const result = {
1394
1433
  properties,
@@ -1397,13 +1436,14 @@ function createPropertyReads(ctx, { store, fonts }) {
1397
1436
  };
1398
1437
  cache = {
1399
1438
  model,
1400
- range,
1439
+ selection,
1401
1440
  v: result
1402
1441
  };
1403
1442
  return result;
1404
1443
  };
1405
1444
  return {
1406
1445
  activeTextRange,
1446
+ activeCaret,
1407
1447
  selectionPropertiesOf
1408
1448
  };
1409
1449
  }
@@ -3084,8 +3124,10 @@ function createStore(ctx, view, records, events, lending) {
3084
3124
  * program the engine writes with. A box someone types in is resolved the way
3085
3125
  * its write will be (the fonts plugin's `resolve`, with the box, so its own
3086
3126
  * font counts): when the edit starts, and again after a pause in typing once
3087
- * the text has a character it wasn't resolved with. Any other box is drawn in
3088
- * the fonts its names say. Without the fonts plugin, a box is drawn in the
3127
+ * the text has a character it wasn't resolved with. An answer holds for the
3128
+ * faces it was asked about: a box restyled since (another font, a bold word)
3129
+ * is asked again, and drawn in the fonts its names say until it is answered.
3130
+ * Any other box is drawn in the fonts its names say. Without the fonts plugin, a box is drawn in the
3089
3131
  * standard font the engine falls back to for its font or face, in that
3090
3132
  * font's built-in program (`@embedpdf/core-fonts`).
3091
3133
  */
@@ -3099,13 +3141,16 @@ const codePointsOf = (text) => Array.from(text, (char) => char.codePointAt(0));
3099
3141
  function createTextFonts(ctx, store, lookup) {
3100
3142
  const fonts = () => ctx.tryGet(_embedpdf_plugin_fonts_contract.FontsToken);
3101
3143
  /**
3102
- * What the engine answered per box: the run each face its text was resolved with drew in, its
3103
- * CSS asked for on each read (it changes as the programs reach the browser).
3144
+ * What the engine answered per box: the faces it was asked about, the record it was asked for
3145
+ * (a newer one with the same faces keeps the answer), and the run each face drew in, its CSS
3146
+ * asked for on each read (it changes as the programs reach the browser).
3104
3147
  */
3105
3148
  const answers = /* @__PURE__ */ new Map();
3106
3149
  /** The box being typed in, and the characters its last resolve asked about. */
3150
+ let editing = null;
3107
3151
  let asked = null;
3108
- let resolving = null;
3152
+ /** Each box's resolve in flight: a newer one for the same box replaces it. */
3153
+ const resolving = /* @__PURE__ */ new Map();
3109
3154
  let cancelPause = () => {};
3110
3155
  let answered = 0;
3111
3156
  let token = {
@@ -3118,13 +3163,13 @@ function createTextFonts(ctx, store, lookup) {
3118
3163
  const face = (0, _embedpdf_engine_core_runtime.faceForFontName)(style.fontFamily, (key) => lookup().find((font) => font.key === key));
3119
3164
  return (face.weight ?? 400) === body.weight && (face.italic ?? false) === body.italic;
3120
3165
  };
3121
- /** Ask the engine what the box's text will be drawn in, and keep the answer. */
3122
- const resolveBox = async (id) => {
3123
- const api = fonts();
3124
- const record = store.model().byId[id];
3125
- const style = record && (0, _embedpdf_core_annotation.textOf)(record.annotation);
3126
- if (!api || !record || !style || record.annotation.subtype !== "free-text") return;
3127
- const ref = (0, _embedpdf_core_annotation.refOf)(record) ?? void 0;
3166
+ /**
3167
+ * What the engine is asked about a box's text: the body first, then every run, unstyled ones by
3168
+ * the box's font, the rest by face; `faces` names the faces asked, whatever the text.
3169
+ */
3170
+ const questionOf = (record) => {
3171
+ const style = (0, _embedpdf_core_annotation.textOf)(record.annotation);
3172
+ if (!style || record.annotation.subtype !== "free-text") return null;
3128
3173
  const doc = (0, _embedpdf_core_annotation.richDocOf)(record.annotation, lookup);
3129
3174
  const body = doc.body;
3130
3175
  const faces = [BODY];
@@ -3152,32 +3197,85 @@ function createTextFonts(ctx, store, lookup) {
3152
3197
  ...face
3153
3198
  });
3154
3199
  }
3155
- asked = {
3200
+ const key = JSON.stringify({
3201
+ body: runs[0],
3202
+ faces: [...new Set(faces)].sort()
3203
+ });
3204
+ return {
3205
+ body,
3206
+ runs,
3207
+ faces,
3208
+ text,
3209
+ key
3210
+ };
3211
+ };
3212
+ /** Ask the engine what the box's text will be drawn in, and keep the answer. */
3213
+ const resolveBox = async (id) => {
3214
+ const api = fonts();
3215
+ const record = store.model().byId[id];
3216
+ const question = api && record && questionOf(record);
3217
+ if (!api || !record || !question) return;
3218
+ const { body, runs, faces, text, key } = question;
3219
+ const ref = (0, _embedpdf_core_annotation.refOf)(record) ?? void 0;
3220
+ if (id === editing) asked = {
3156
3221
  id,
3157
3222
  codePoints: new Set(codePointsOf(text))
3158
3223
  };
3159
- resolving?.abort();
3224
+ resolving.get(id)?.abort();
3160
3225
  const request = new AbortController();
3161
- resolving = request;
3226
+ resolving.set(id, request);
3162
3227
  try {
3163
3228
  await api.ensureLoaded();
3164
3229
  const answer = await api.resolve({
3165
3230
  ...ref ? { annotation: ref } : {},
3166
3231
  runs
3167
3232
  }, { signal: request.signal });
3168
- if (resolving !== request) return;
3233
+ if (resolving.get(id) !== request) return;
3234
+ resolving.delete(id);
3169
3235
  const byFace = /* @__PURE__ */ new Map();
3170
3236
  answer.runs.forEach((run, index) => {
3171
3237
  const face = faces[index];
3172
3238
  if (!byFace.has(face)) byFace.set(face, run);
3173
3239
  });
3174
3240
  if (!byFace.has(faceKey(body))) byFace.set(faceKey(body), byFace.get(BODY));
3175
- answers.set(id, byFace);
3241
+ answers.set(id, {
3242
+ faces: key,
3243
+ record,
3244
+ byFace
3245
+ });
3176
3246
  answered++;
3177
3247
  ctx.notify();
3178
3248
  } catch {}
3179
3249
  };
3180
- let editing = null;
3250
+ /**
3251
+ * Drop the answers the model moved on from: a box deleted, or restyled so its faces aren't the
3252
+ * ones asked about, which is asked again (typing keeps the faces, and the answer). The model is
3253
+ * watched, not this plugin's state: a change that shows before the engine answers it, as a
3254
+ * format on a whole box does, changes the view alone.
3255
+ */
3256
+ const dropStaleAnswers = (model) => {
3257
+ let dropped = false;
3258
+ for (const [id, answer] of answers) {
3259
+ const record = model.byId[id];
3260
+ if (record === answer.record) continue;
3261
+ const question = record && questionOf(record);
3262
+ if (question && question.key === answer.faces) {
3263
+ answers.set(id, {
3264
+ ...answer,
3265
+ record
3266
+ });
3267
+ continue;
3268
+ }
3269
+ answers.delete(id);
3270
+ dropped = true;
3271
+ if (question) resolveBox(id);
3272
+ }
3273
+ if (dropped) {
3274
+ answered++;
3275
+ ctx.notify();
3276
+ }
3277
+ };
3278
+ ctx.watch(store.model, dropStaleAnswers);
3181
3279
  ctx.state.onChange(() => {
3182
3280
  const next = store.model().editing;
3183
3281
  if (next === editing) return;
@@ -3190,15 +3288,15 @@ function createTextFonts(ctx, store, lookup) {
3190
3288
  bodyCss: (id, style, body) => {
3191
3289
  const api = fonts();
3192
3290
  const byFont = bodyIsItsFont(style, body);
3193
- if (!api) return (0, _embedpdf_core_fonts.standardFontCss)(byFont ? (0, _embedpdf_core_fonts.standardFontOf)(style.fontFamily) : (0, _embedpdf_core_fonts.standardFontForFace)(body));
3194
- const run = answers.get(id)?.get(BODY);
3291
+ if (!api) return (0, _embedpdf_core_fonts.standardFontCss)(byFont ? (0, _embedpdf_core_fonts.standardFontOf)(style.fontFamily) : (0, _embedpdf_engine_core_runtime.standardFontForFace)(body));
3292
+ const run = answers.get(id)?.byFace.get(BODY);
3195
3293
  if (run) return api.getRunCss(run);
3196
3294
  return byFont ? api.getFontCss(style.fontFamily) : api.getFaceCss(body);
3197
3295
  },
3198
3296
  faceCss: (id, face) => {
3199
3297
  const api = fonts();
3200
- if (!api) return (0, _embedpdf_core_fonts.standardFontCss)((0, _embedpdf_core_fonts.standardFontForFace)(face));
3201
- const run = id !== null ? answers.get(id)?.get(faceKey(face)) : void 0;
3298
+ if (!api) return (0, _embedpdf_core_fonts.standardFontCss)((0, _embedpdf_engine_core_runtime.standardFontForFace)(face));
3299
+ const run = id !== null ? answers.get(id)?.byFace.get(faceKey(face)) : void 0;
3202
3300
  return run ? api.getRunCss(run) : api.getFaceCss(face);
3203
3301
  },
3204
3302
  familyOfCss: (cssFamily) => {
@@ -4762,26 +4860,28 @@ function createSelectionWrites(ctx, { store, authority, fonts, behaviors, lendin
4762
4860
  * member takes the fields its kind has, and the change shows at once, as
4763
4861
  * one change. While the text editor holds a range, the font, size, colour
4764
4862
  * and formats restyle the runs it covers (a delta over the body, through
4765
- * the pure run algebra) as part of the typing, and the rest goes to the
4766
- * annotation: its change sends the typing first, so the body it writes is
4767
- * the latest.
4863
+ * the pure run algebra) as part of the typing; at its caret they are the
4864
+ * style of the text typed next, and nothing is written. The rest goes to
4865
+ * the annotation: its change sends the typing first, so the body it writes
4866
+ * is the latest.
4768
4867
  */
4769
4868
  const restyle = (patch) => {
4770
4869
  const model = store.model();
4771
- const range = properties.activeTextRange(model);
4870
+ const editor = properties.activeTextRange(model) ?? properties.activeCaret(model);
4772
4871
  const commits = [];
4773
4872
  const patches = {};
4774
4873
  for (const id of model.selected) {
4775
4874
  const record = model.byId[id];
4776
4875
  if (!record) continue;
4777
4876
  const own = typeof patch === "function" ? patch(record.annotation) : patch;
4778
- if (range?.id !== id) {
4877
+ if (editor?.id !== id) {
4779
4878
  patches[id] = own;
4780
4879
  continue;
4781
4880
  }
4782
4881
  const { delta, rest } = runDeltaForFields(own, fonts);
4783
- if (Object.keys(delta).length) {
4784
- const next = (0, _embedpdf_core_annotation.applyStyleToRange)({ paragraphs: (0, _embedpdf_core_annotation.richDocOf)(record.annotation, fonts).paragraphs }, range, delta);
4882
+ if (Object.keys(delta).length && editor.start === editor.end) text.styleTyping(id, delta);
4883
+ else if (Object.keys(delta).length) {
4884
+ const next = (0, _embedpdf_core_annotation.applyStyleToRange)({ paragraphs: (0, _embedpdf_core_annotation.richDocOf)(record.annotation, fonts).paragraphs }, editor, delta);
4785
4885
  commits.push(text.type(id, {
4786
4886
  type: "setRichText",
4787
4887
  id,
@@ -4805,12 +4905,15 @@ function createSelectionWrites(ctx, { store, authority, fonts, behaviors, lendin
4805
4905
  });
4806
4906
  };
4807
4907
  /**
4808
- * Bold, italic or underline on or off: the words selected while typing, or
4809
- * the whole of each selected text box once the text being typed has landed.
4908
+ * Bold, italic or underline on or off: the words selected while typing, the
4909
+ * text typed next at the caret, or all the text of each selected box once
4910
+ * the text being typed has landed. On, unless all of it already has it.
4810
4911
  */
4811
4912
  const toggleFormat = async (format, options = {}) => {
4812
- const on = properties.selectionPropertiesOf().values[format] !== true;
4813
- const commits = properties.activeTextRange(store.model()) ? restyle({ [format]: on }) : [store.commit({
4913
+ const { values, mixed } = properties.selectionPropertiesOf();
4914
+ const on = values[format] !== true || mixed.includes(format);
4915
+ const model = store.model();
4916
+ const commits = properties.activeTextRange(model) ?? properties.activeCaret(model) ? restyle({ [format]: on }) : [store.commit({
4814
4917
  type: "setTextFormat",
4815
4918
  format,
4816
4919
  on
@@ -5186,7 +5289,8 @@ function createStamps(ctx, { store, geometry, tools, filePicker, afterCreate })
5186
5289
  * hold): the view shows the text at once, and the change keeps its place in
5187
5290
  * line holding the latest text. It is sent after a pause in typing, when
5188
5291
  * editing ends, when anything else is staged, and before a download; what is
5189
- * typed after that goes into a new one.
5292
+ * typed after that goes into a new one. A style chosen at the caret waits in
5293
+ * the editor's selection for the text typed there next.
5190
5294
  */
5191
5295
  const TEXT_WRITE_DELAY_MS = 250;
5192
5296
  function createTextEditing(ctx, { store, fonts, textFonts }, chrome) {
@@ -5223,6 +5327,44 @@ function createTextEditing(ctx, { store, fonts, textFonts }, chrome) {
5223
5327
  });
5224
5328
  return commit;
5225
5329
  };
5330
+ /**
5331
+ * The style the text typed next at the caret in box `id` takes, over what
5332
+ * was chosen there already. Nothing is written until it is typed.
5333
+ */
5334
+ const styleTyping = (id, style) => {
5335
+ const selection = ctx.state.get().textSelection;
5336
+ if (selection?.id !== id || selection.start !== selection.end) return;
5337
+ ctx.state.update(setTextSelection, {
5338
+ ...selection,
5339
+ typing: {
5340
+ ...selection.typing,
5341
+ ...style
5342
+ }
5343
+ });
5344
+ };
5345
+ /**
5346
+ * The paragraphs typed into box `id`, the text typed at the caret in the
5347
+ * style chosen there (`typeWithStyle`), and the caret where the typing
5348
+ * leaves it. Any other edit drops the style.
5349
+ */
5350
+ const typedInStyle = (id, paragraphs) => {
5351
+ const selection = ctx.state.get().textSelection;
5352
+ const record = store.model().byId[id];
5353
+ if (!selection?.typing || selection.id !== id || !record) return paragraphs;
5354
+ const typed = (0, _embedpdf_core_annotation.typeWithStyle)((0, _embedpdf_core_annotation.richDocOf)(record.annotation, fonts), { paragraphs }, selection.start, selection.typing);
5355
+ const caret = typed ? {
5356
+ id,
5357
+ start: typed.caret,
5358
+ end: typed.caret,
5359
+ ...typed.style && { typing: typed.style }
5360
+ } : {
5361
+ id,
5362
+ start: selection.start,
5363
+ end: selection.end
5364
+ };
5365
+ ctx.state.update(setTextSelection, caret);
5366
+ return typed?.doc.paragraphs ?? paragraphs;
5367
+ };
5226
5368
  store.onEffect("text", (effect, model) => {
5227
5369
  const record = model.byId[effect.id];
5228
5370
  const ref = (0, _embedpdf_core_annotation.refOf)(record);
@@ -5238,6 +5380,7 @@ function createTextEditing(ctx, { store, fonts, textFonts }, chrome) {
5238
5380
  });
5239
5381
  return {
5240
5382
  type,
5383
+ styleTyping,
5241
5384
  sendTyping,
5242
5385
  text: {
5243
5386
  begin: (ref) => {
@@ -5280,11 +5423,13 @@ function createTextEditing(ctx, { store, fonts, textFonts }, chrome) {
5280
5423
  },
5281
5424
  draftRichText: (ref, doc) => {
5282
5425
  const id = (0, _embedpdf_engine_core_runtime.annotationKey)(ref);
5426
+ const kept = { paragraphs: typedInStyle(id, doc.paragraphs) };
5283
5427
  type(id, {
5284
5428
  type: "setRichText",
5285
5429
  id,
5286
- doc: { paragraphs: doc.paragraphs }
5430
+ doc: kept
5287
5431
  });
5432
+ return kept;
5288
5433
  },
5289
5434
  setTextSelection: (ref, range) => {
5290
5435
  const id = (0, _embedpdf_engine_core_runtime.annotationKey)(ref);