@bsky.app/tapper 0.4.2 → 0.4.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @bsky.app/tapper
2
2
 
3
+ ## 0.4.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [`047391d`](https://github.com/bluesky-social/toolbox/commit/047391df8efff5be9b176eae32ab1a879e7290cb) Thanks [@estrattonbailey](https://github.com/estrattonbailey)! - Handle fixes for web
8
+
9
+ - [`6ef6682`](https://github.com/bluesky-social/toolbox/commit/6ef6682a0d2c877a62bf0fc49452e8ce3b203cd2) Thanks [@estrattonbailey](https://github.com/estrattonbailey)! - Move to uncontrolled input for better old-arch support
10
+
11
+ - [`018d8d3`](https://github.com/bluesky-social/toolbox/commit/018d8d39e434d7f9b5d0e1b53e4f8528cb188665) Thanks [@estrattonbailey](https://github.com/estrattonbailey)! - Only use debounce hack if required by platform
12
+
3
13
  ## 0.4.2
4
14
 
5
15
  ### Patch Changes
package/build/index.d.ts CHANGED
@@ -10,12 +10,13 @@ export declare class Tapper {
10
10
  selection: TapperSelection;
11
11
  nodes: TapperNode[];
12
12
  activeFacet: TapperActiveFacet | null;
13
- private pendingMutation;
14
- private static MUTATION_DEBOUNCE_MS;
13
+ private inputRef;
15
14
  private updating;
16
15
  private storeListeners;
17
16
  private snapshot;
18
17
  constructor(config?: TapperConfig);
18
+ setInputRef: (node: TextInput | null) => void;
19
+ private setInputSelection;
19
20
  subscribe: (listener: () => void) => () => boolean;
20
21
  getSnapshot: () => TapperSnapshot;
21
22
  on: <K extends keyof TapperEvents>(event: K, cb: (data: TapperEvents[K]) => void) => () => void;
@@ -46,8 +47,7 @@ export declare function useTapper(config: TapperConfig): {
46
47
  };
47
48
  inputProps: {
48
49
  ref: (node: TextInput | null) => void;
49
- value: string;
50
- selection: TapperSelection;
50
+ value: string | undefined;
51
51
  onChangeText: (newText: string) => void;
52
52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ED,OAAO,CAAC,iBAAiB;IAUzB,SAAS,GAAI,UAAU,MAAM,IAAI,mBAGhC;IAED,WAAW,QAAO,cAAc,CAE/B;IAED,EAAE,GAAI,CAAC,SAAS,MAAM,YAAY,EAChC,OAAO,CAAC,EACR,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,IAAI,gBAOpC;IAED,OAAO,CAAC,IAAI;IAIZ,OAAO,CAAC,MAAM;IAUd,OAAO,CAAC,MAAM;IAmEd,gBAAgB,GAAI,SAAS,MAAM,UA6BlC;IAED,qBAAqB,GAAI,GAAG;QAC1B,WAAW,EAAE;YAAC,SAAS,EAAE;gBAAC,KAAK,EAAE,MAAM,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAA;aAAC,CAAA;SAAC,CAAA;KACvD,UA+BA;IAED,WAAW,GAAI,MAAM,MAAM,EAAE,SAAS,MAAM,UA+B3C;IAED,OAAO,CAAC,OAAO,CAsBd;IAED,MAAM,GAAI,MAAM,MAAM,UAMrB;CACF;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,YAAY;;SAhOtC,CAAC,SAAS,MAAM,YAAY,2CAED,IAAI;mBAqNrB,MAAM;;;;;;;oBAgBb,SAAS,GAAG,IAAI;;gCA3II,MAAM;+BA+BP;YAC1B,WAAW,EAAE;gBAAC,SAAS,EAAE;oBAAC,KAAK,EAAE,MAAM,CAAC;oBAAC,GAAG,EAAE,MAAM,CAAA;iBAAC,CAAA;aAAC,CAAA;SACvD;;EAoIF"}
package/build/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import { useCallback, useRef, useState, useSyncExternalStore } from 'react';
2
+ import { Platform } from 'react-native';
2
3
  import { parseNodesFromText, detectActiveFacet, deriveTriggers, nodeToFacet, compileFacetRegexes, } from './util';
3
4
  import * as defaultFacets from './facets';
4
5
  export * from './types';
5
6
  export * as facets from './facets';
7
+ const IS_WEB = Platform.OS === 'web';
6
8
  export class Tapper {
7
9
  facetRegexes;
8
10
  triggers;
@@ -14,30 +16,7 @@ export class Tapper {
14
16
  selection = { start: 0, end: 0 };
15
17
  nodes = [];
16
18
  activeFacet = null;
17
- /*
18
- * Guard against stale native events after programmatic text replacement.
19
- *
20
- * After insert() or atomic deletion, we update this.text and this.selection
21
- * synchronously. However, iOS (especially on the old architecture bridge)
22
- * may then fire stale onChangeText and onSelectionChange events reflecting
23
- * the *previous* text/cursor state. These arrive within ~60ms.
24
- *
25
- * We record the expected textLength and a timestamp, then use two guards:
26
- *
27
- * 1. handleTextChange: if the incoming text length doesn't match the
28
- * expected length AND we're within the debounce window, it's a stale
29
- * event echoing the pre-replacement text. Skip it.
30
- *
31
- * 2. handleSelectionChange: if we're within the debounce window, skip
32
- * ALL selection events. The correct selection was already set by
33
- * update(), and any events within the window are either duplicates
34
- * (no-ops) or stale positions from the old text.
35
- *
36
- * After the window passes, the guard clears itself and normal event
37
- * handling resumes (cursor taps, typing, etc).
38
- */
39
- pendingMutation = null;
40
- static MUTATION_DEBOUNCE_MS = 200;
19
+ inputRef = null;
41
20
  updating = false;
42
21
  // useSyncExternalStore plumbing
43
22
  storeListeners = new Set();
@@ -56,6 +35,20 @@ export class Tapper {
56
35
  this.replaceText(config.initialText);
57
36
  }
58
37
  }
38
+ setInputRef = (node) => {
39
+ this.inputRef = node;
40
+ };
41
+ setInputSelection(start, end) {
42
+ const el = this.inputRef;
43
+ if (!el)
44
+ return;
45
+ if (typeof el.setSelection === 'function') {
46
+ el.setSelection(start, end);
47
+ }
48
+ else if (typeof el.setSelectionRange === 'function') {
49
+ el.setSelectionRange(start, end);
50
+ }
51
+ }
59
52
  subscribe = (listener) => {
60
53
  this.storeListeners.add(listener);
61
54
  return () => this.storeListeners.delete(listener);
@@ -136,22 +129,10 @@ export class Tapper {
136
129
  handleTextChange = (newText) => {
137
130
  if (newText === this.text)
138
131
  return;
139
- /*
140
- * Guard 1: stale text. After insert/atomic deletion, iOS may echo
141
- * onChangeText with the pre-replacement text. If the incoming length
142
- * doesn't match what we expect and we're within the debounce window,
143
- * this is a stale echo — skip it.
144
- */
145
- if (this.pendingMutation !== null) {
146
- const elapsed = Date.now() - this.pendingMutation.timestamp;
147
- if (elapsed < Tapper.MUTATION_DEBOUNCE_MS &&
148
- newText.length !== this.pendingMutation.textLength) {
149
- return;
150
- }
151
- }
152
132
  const diff = newText.length - this.text.length;
153
133
  let newEnd = Math.max(this.selection.end + diff, 0);
154
134
  // Atomic deletion: backspace into a facet from outside
135
+ let didAtomicDelete = false;
155
136
  if (diff === -1 && newEnd < this.selection.end) {
156
137
  for (const node of this.nodes) {
157
138
  if (node.type === 'facet' &&
@@ -161,37 +142,18 @@ export class Tapper {
161
142
  newText =
162
143
  newText.slice(0, node.start) + newText.slice(node.start + remnant);
163
144
  newEnd = node.start;
164
- this.pendingMutation = {
165
- textLength: newText.length,
166
- timestamp: Date.now(),
167
- };
145
+ didAtomicDelete = true;
168
146
  break;
169
147
  }
170
148
  }
171
149
  }
172
150
  this.update(newText, { start: newEnd, end: newEnd });
151
+ if (didAtomicDelete) {
152
+ this.setInputSelection(newEnd, newEnd);
153
+ }
173
154
  };
174
155
  handleSelectionChange = (e) => {
175
156
  const { start, end } = e.nativeEvent.selection;
176
- /*
177
- * Guard 2: stale selection. After insert/atomic deletion, iOS may fire
178
- * onSelectionChange with cursor positions from the old text state
179
- * (e.g. a "deselection" event at the pre-insert cursor). Within the
180
- * debounce window, skip ALL selection events — the correct selection
181
- * was already set synchronously by update().
182
- */
183
- if (this.pendingMutation !== null) {
184
- const elapsed = Date.now() - this.pendingMutation.timestamp;
185
- if (elapsed < Tapper.MUTATION_DEBOUNCE_MS)
186
- return;
187
- this.pendingMutation = null;
188
- }
189
- /*
190
- * Guard 3: premature selection. iOS sometimes fires onSelectionChange
191
- * before onChangeText for the same edit, reporting a cursor position
192
- * that's beyond the current text length. Skip it — handleTextChange
193
- * will set the correct position when it arrives.
194
- */
195
157
  if (end > this.text.length)
196
158
  return;
197
159
  if (start === this.selection.start && end === this.selection.end)
@@ -263,21 +225,15 @@ export class Tapper {
263
225
  };
264
226
  this.emit('afterInsert', this.activeFacet);
265
227
  }
266
- this.pendingMutation = {
267
- textLength: newText.length,
268
- timestamp: Date.now(),
269
- };
270
228
  this.update(newText, { start: newEnd, end: newEnd });
229
+ this.setInputSelection(newEnd, newEnd);
271
230
  };
272
231
  insert = (text) => {
273
232
  const pos = this.selection.end;
274
233
  const newText = this.text.slice(0, pos) + text + this.text.slice(pos);
275
234
  const newEnd = pos + text.length;
276
- this.pendingMutation = {
277
- textLength: newText.length,
278
- timestamp: Date.now(),
279
- };
280
235
  this.update(newText, { start: newEnd, end: newEnd });
236
+ this.setInputSelection(newEnd, newEnd);
281
237
  };
282
238
  }
283
239
  export function useTapper(config) {
@@ -287,8 +243,9 @@ export function useTapper(config) {
287
243
  const inputRef = useRef(null);
288
244
  const handleSetInput = useCallback((node) => {
289
245
  inputRef.current = node;
246
+ store.setInputRef(node);
290
247
  setInput(node);
291
- }, []);
248
+ }, [store]);
292
249
  const focus = useCallback(() => input?.focus(), [input]);
293
250
  const blur = useCallback(() => input?.blur(), [input]);
294
251
  return {
@@ -302,8 +259,7 @@ export function useTapper(config) {
302
259
  },
303
260
  inputProps: {
304
261
  ref: handleSetInput,
305
- value: state.text,
306
- selection: state.selection,
262
+ value: IS_WEB ? state.text : undefined,
307
263
  onChangeText: store.handleTextChange,
308
264
  onSelectionChange: store.handleSelectionChange,
309
265
  },
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAC,MAAM,OAAO,CAAA;AAWzE,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,cAAc,EACd,WAAW,EACX,mBAAmB,GAEpB,MAAM,QAAQ,CAAA;AACf,OAAO,KAAK,aAAa,MAAM,UAAU,CAAA;AAEzC,cAAc,SAAS,CAAA;AACvB,OAAO,KAA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C,IAAsB,EAAE,EAAE;QAC5D,QAAQ,CAAC,OAAO,GAAG,IAAI,CAAA;QACvB,QAAQ,CAAC,IAAI,CAAC,CAAA;IAChB,CAAC,EAAE,EAAE,CAAC,CAAA;IACN,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAA;IACxD,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAA;IAEtD,OAAO;QACL,KAAK;QACL,EAAE,EAAE,KAAK,CAAC,EAAE;QACZ,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,KAAK,EAAE;YACL,OAAO,EAAE,KAAK;YACd,KAAK;YACL,IAAI;SACL;QACD,UAAU,EAAE;YACV,GAAG,EAAE,cAAc;YACnB,KAAK,EAAE,KAAK,CAAC,IAAI;YACjB,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,YAAY,EAAE,KAAK,CAAC,gBAAgB;YACpC,iBAAiB,EAAE,KAAK,CAAC,qBAAqB;SAC/C;KACF,CAAA;AACH,CAAC","sourcesContent":["import {useCallback, useRef, useState, useSyncExternalStore} from 'react'\nimport {TextInput} from 'react-native'\n\nimport {\n TapperConfig,\n TapperEvents,\n TapperNode,\n TapperActiveFacet,\n TapperSelection,\n TapperSnapshot,\n} from './types'\nimport {\n parseNodesFromText,\n detectActiveFacet,\n deriveTriggers,\n nodeToFacet,\n compileFacetRegexes,\n type CompiledFacetRegexes,\n} from './util'\nimport * as defaultFacets from './facets'\n\nexport * from './types'\nexport * as facets from './facets'\n\nexport class Tapper {\n private facetRegexes: CompiledFacetRegexes\n private triggers: Map<string, string>\n\n // Event emitters\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private listeners = new Map<keyof TapperEvents, Set<(data: any) => void>>()\n\n // Public state\n text = ''\n selection: TapperSelection = {start: 0, end: 0}\n nodes: TapperNode[] = []\n activeFacet: TapperActiveFacet | null = null\n\n /*\n * Guard against stale native events after programmatic text replacement.\n *\n * After insert() or atomic deletion, we update this.text and this.selection\n * synchronously. However, iOS (especially on the old architecture bridge)\n * may then fire stale onChangeText and onSelectionChange events reflecting\n * the *previous* text/cursor state. These arrive within ~60ms.\n *\n * We record the expected textLength and a timestamp, then use two guards:\n *\n * 1. handleTextChange: if the incoming text length doesn't match the\n * expected length AND we're within the debounce window, it's a stale\n * event echoing the pre-replacement text. Skip it.\n *\n * 2. handleSelectionChange: if we're within the debounce window, skip\n * ALL selection events. The correct selection was already set by\n * update(), and any events within the window are either duplicates\n * (no-ops) or stale positions from the old text.\n *\n * After the window passes, the guard clears itself and normal event\n * handling resumes (cursor taps, typing, etc).\n */\n private pendingMutation: {textLength: number; timestamp: number} | null = null\n private static MUTATION_DEBOUNCE_MS = 200\n\n private updating = false\n\n // useSyncExternalStore plumbing\n private storeListeners = new Set<() => void>()\n private snapshot: TapperSnapshot\n\n constructor(config?: TapperConfig) {\n const facets = config?.facets || defaultFacets\n this.facetRegexes = compileFacetRegexes(facets)\n this.triggers = deriveTriggers(facets)\n this.snapshot = {\n text: this.text,\n selection: this.selection,\n nodes: this.nodes,\n activeFacet: this.activeFacet,\n }\n if (config?.initialText) {\n this.replaceText(config.initialText)\n }\n }\n\n subscribe = (listener: () => void) => {\n this.storeListeners.add(listener)\n return () => this.storeListeners.delete(listener)\n }\n\n getSnapshot = (): TapperSnapshot => {\n return this.snapshot\n }\n\n on = <K extends keyof TapperEvents>(\n event: K,\n cb: (data: TapperEvents[K]) => void,\n ) => {\n if (!this.listeners.has(event)) this.listeners.set(event, new Set())\n this.listeners.get(event)!.add(cb)\n return () => {\n this.listeners.get(event)?.delete(cb)\n }\n }\n\n private emit<K extends keyof TapperEvents>(event: K, data: TapperEvents[K]) {\n this.listeners.get(event)?.forEach(cb => cb(data))\n }\n\n private notify() {\n this.snapshot = {\n text: this.text,\n selection: this.selection,\n nodes: this.nodes,\n activeFacet: this.activeFacet,\n }\n this.storeListeners.forEach(l => l())\n }\n\n private update(newText: string, selection: TapperSelection) {\n // guard against circular updates when insert() is called during an update cycle\n if (this.updating) return\n this.updating = true\n\n const cursor = selection.end\n const isRange = selection.start !== selection.end\n\n const nodes =\n newText === this.text\n ? this.nodes\n : parseNodesFromText(\n newText,\n this.facetRegexes,\n this.nodes,\n cursor,\n this.triggers,\n )\n const prev = this.activeFacet\n const detected = isRange\n ? null\n : detectActiveFacet(nodes, newText, cursor, this.triggers)\n\n // When cursor leaves a facet, commit it\n let committedNode: TapperNode | null = null\n if (prev && (!detected || detected.type !== prev.type)) {\n for (const node of nodes) {\n if (\n node.type === 'facet' &&\n node.facetType === prev.type &&\n node.start === prev.range.start &&\n node.end === prev.range.end\n ) {\n node.committed = true\n committedNode = node\n break\n }\n }\n }\n\n // Build activeFacet with insert() baked in\n const active: TapperActiveFacet | null = detected\n ? {...detected, replace: this.replace}\n : null\n\n this.text = newText\n this.selection = selection\n this.nodes = nodes\n this.activeFacet = active\n\n this.updating = false\n this.notify()\n\n // Fire events after state is finalized\n const facetChanged =\n active?.type !== prev?.type ||\n active?.value !== prev?.value ||\n active?.range.start !== prev?.range.start ||\n active?.range.end !== prev?.range.end\n if (facetChanged) {\n this.emit('activeFacet', active)\n }\n if (committedNode) {\n this.emit('facetCommitted', nodeToFacet(committedNode))\n }\n }\n\n handleTextChange = (newText: string) => {\n if (newText === this.text) return\n\n /*\n * Guard 1: stale text. After insert/atomic deletion, iOS may echo\n * onChangeText with the pre-replacement text. If the incoming length\n * doesn't match what we expect and we're within the debounce window,\n * this is a stale echo — skip it.\n */\n if (this.pendingMutation !== null) {\n const elapsed = Date.now() - this.pendingMutation.timestamp\n if (\n elapsed < Tapper.MUTATION_DEBOUNCE_MS &&\n newText.length !== this.pendingMutation.textLength\n ) {\n return\n }\n }\n\n const diff = newText.length - this.text.length\n let newEnd = Math.max(this.selection.end + diff, 0)\n\n // Atomic deletion: backspace into a facet from outside\n if (diff === -1 && newEnd < this.selection.end) {\n for (const node of this.nodes) {\n if (\n node.type === 'facet' &&\n node.committed &&\n this.selection.end === node.end\n ) {\n const remnant = node.end - node.start - 1\n newText =\n newText.slice(0, node.start) + newText.slice(node.start + remnant)\n newEnd = node.start\n this.pendingMutation = {\n textLength: newText.length,\n timestamp: Date.now(),\n }\n break\n }\n }\n }\n\n this.update(newText, {start: newEnd, end: newEnd})\n }\n\n handleSelectionChange = (e: {\n nativeEvent: {selection: {start: number; end: number}}\n }) => {\n const {start, end} = e.nativeEvent.selection\n\n /*\n * Guard 2: stale selection. After insert/atomic deletion, iOS may fire\n * onSelectionChange with cursor positions from the old text state\n * (e.g. a \"deselection\" event at the pre-insert cursor). Within the\n * debounce window, skip ALL selection events — the correct selection\n * was already set synchronously by update().\n */\n if (this.pendingMutation !== null) {\n const elapsed = Date.now() - this.pendingMutation.timestamp\n if (elapsed < Tapper.MUTATION_DEBOUNCE_MS) return\n this.pendingMutation = null\n }\n\n /*\n * Guard 3: premature selection. iOS sometimes fires onSelectionChange\n * before onChangeText for the same edit, reporting a cursor position\n * that's beyond the current text length. Skip it — handleTextChange\n * will set the correct position when it arrives.\n */\n if (end > this.text.length) return\n\n if (start === this.selection.start && end === this.selection.end) return\n\n this.selection = {start, end}\n\n /*\n * For selection-only changes (no text change), check if the active facet\n * would change. If not, skip the full update() cycle — just update the\n * selection in the snapshot.\n */\n const isRange = start !== end\n const detected = isRange\n ? null\n : detectActiveFacet(this.nodes, this.text, end, this.triggers)\n const prev = this.activeFacet\n const facetChanged =\n detected?.type !== prev?.type ||\n detected?.value !== prev?.value ||\n detected?.range.start !== prev?.range.start ||\n detected?.range.end !== prev?.range.end\n if (!facetChanged) {\n this.snapshot = {...this.snapshot, selection: this.selection}\n this.storeListeners.forEach(l => l())\n return\n }\n\n this.update(this.text, {start, end})\n }\n\n replaceText = (text: string, cursor?: number) => {\n const prevActive = this.activeFacet\n const nodes = parseNodesFromText(text, this.facetRegexes)\n // Mark all non-text nodes as committed (pre-existing facets)\n const newlyCommitted: TapperNode[] = []\n for (const node of nodes) {\n if (node.type === 'facet') {\n node.committed = true\n newlyCommitted.push(node)\n }\n }\n\n const pos = cursor ?? text.length\n const detected = detectActiveFacet(nodes, text, pos, this.triggers)\n const active: TapperActiveFacet | null = detected\n ? {...detected, replace: this.replace}\n : null\n\n this.text = text\n this.selection = {start: pos, end: pos}\n this.nodes = nodes\n this.activeFacet = active\n\n this.notify()\n\n if (active !== prevActive) {\n this.emit('activeFacet', active)\n }\n for (const node of newlyCommitted) {\n this.emit('facetCommitted', nodeToFacet(node))\n }\n }\n\n private replace = (value: string, options?: {noTrailingSpace?: boolean}) => {\n if (!this.activeFacet) return\n\n const replacement = value + (options?.noTrailingSpace ? '' : ' ')\n const newText =\n this.text.slice(0, this.activeFacet.range.start) +\n replacement +\n this.text.slice(this.activeFacet.range.end)\n const newEnd = this.activeFacet.range.start + replacement.length\n\n if (this.activeFacet) {\n this.activeFacet.raw = value\n this.activeFacet.value = value\n this.activeFacet.range = {\n start: this.activeFacet.range.start,\n end: this.activeFacet.range.start + value.length,\n }\n this.emit('afterInsert', this.activeFacet)\n }\n\n this.pendingMutation = {\n textLength: newText.length,\n timestamp: Date.now(),\n }\n this.update(newText, {start: newEnd, end: newEnd})\n }\n\n insert = (text: string) => {\n const pos = this.selection.end\n const newText = this.text.slice(0, pos) + text + this.text.slice(pos)\n const newEnd = pos + text.length\n this.pendingMutation = {\n textLength: newText.length,\n timestamp: Date.now(),\n }\n this.update(newText, {start: newEnd, end: newEnd})\n }\n}\n\nexport function useTapper(config: TapperConfig) {\n const [store] = useState(() => new Tapper(config))\n const state = useSyncExternalStore(store.subscribe, store.getSnapshot)\n const [input, setInput] = useState<TextInput | null>(null)\n const inputRef = useRef<{focus(): void}>(null)\n\n const handleSetInput = useCallback((node: TextInput | null) => {\n inputRef.current = node\n setInput(node)\n }, [])\n const focus = useCallback(() => input?.focus(), [input])\n const blur = useCallback(() => input?.blur(), [input])\n\n return {\n state,\n on: store.on,\n insert: store.insert,\n input: {\n element: input,\n focus,\n blur,\n },\n inputProps: {\n ref: handleSetInput,\n value: state.text,\n selection: state.selection,\n onChangeText: store.handleTextChange,\n onSelectionChange: store.handleSelectionChange,\n },\n }\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAC,MAAM,OAAO,CAAA;AACzE,OAAO,EAAC,QAAQ,EAAY,MAAM,cAAc,CAAA;AAUhD,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,cAAc,EACd,WAAW,EACX,mBAAmB,GAEpB,MAAM,QAAQ,CAAA;AACf,OAAO,KAAK,aAAa,MAAM,UAAU,CAAA;AAEzC,cAAc,SAAS,CAAA;AACvB,OAAO,KAAK,MAAM,MAAM,UAAU,CAAA;AAElC,MAAM,MAAM,GAAG,QAAQ,CAAC,EAAE,KAAK,KAAK,CAAA;AAEpC,MAAM,OAAO,MAAM;IACT,YAAY,CAAsB;IAClC,QAAQ,CAAqB;IAErC,iBAAiB;IACjB,8DAA8D;IACtD,SAAS,GAAG,IAAI,GAAG,EAAgD,CAAA;IAE3E,eAAe;IACf,IAAI,GAAG,EAAE,CAAA;IACT,SAAS,GAAoB,EAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAC,CAAA;IAC/C,KAAK,GAAiB,EAAE,CAAA;IACxB,WAAW,GAA6B,IAAI,CAAA;IAEpC,QAAQ,GAAqB,IAAI,CAAA;IACjC,QAAQ,GAAG,KAAK,CAAA;IAExB,gCAAgC;IACxB,cAAc,GAAG,IAAI,GAAG,EAAc,CAAA;IACtC,QAAQ,CAAgB;IAEhC,YAAY,MAAqB;QAC/B,MAAM,MAAM,GAAG,MAAM,EAAE,MAAM,IAAI,aAAa,CAAA;QAC9C,IAAI,CAAC,YAAY,GAAG,mBAAm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useSyncExternalStore} from 'react'\nimport {Platform, TextInput} from 'react-native'\n\nimport {\n TapperConfig,\n TapperEvents,\n TapperNode,\n TapperActiveFacet,\n TapperSelection,\n TapperSnapshot,\n} from './types'\nimport {\n parseNodesFromText,\n detectActiveFacet,\n deriveTriggers,\n nodeToFacet,\n compileFacetRegexes,\n type CompiledFacetRegexes,\n} from './util'\nimport * as defaultFacets from './facets'\n\nexport * from './types'\nexport * as facets from './facets'\n\nconst IS_WEB = Platform.OS === 'web'\n\nexport class Tapper {\n private facetRegexes: CompiledFacetRegexes\n private triggers: Map<string, string>\n\n // Event emitters\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private listeners = new Map<keyof TapperEvents, Set<(data: any) => void>>()\n\n // Public state\n text = ''\n selection: TapperSelection = {start: 0, end: 0}\n nodes: TapperNode[] = []\n activeFacet: TapperActiveFacet | null = null\n\n private inputRef: TextInput | null = null\n private updating = false\n\n // useSyncExternalStore plumbing\n private storeListeners = new Set<() => void>()\n private snapshot: TapperSnapshot\n\n constructor(config?: TapperConfig) {\n const facets = config?.facets || defaultFacets\n this.facetRegexes = compileFacetRegexes(facets)\n this.triggers = deriveTriggers(facets)\n this.snapshot = {\n text: this.text,\n selection: this.selection,\n nodes: this.nodes,\n activeFacet: this.activeFacet,\n }\n if (config?.initialText) {\n this.replaceText(config.initialText)\n }\n }\n\n setInputRef = (node: TextInput | null) => {\n this.inputRef = node\n }\n\n private setInputSelection(start: number, end: number) {\n const el = this.inputRef as any\n if (!el) return\n if (typeof el.setSelection === 'function') {\n el.setSelection(start, end)\n } else if (typeof el.setSelectionRange === 'function') {\n el.setSelectionRange(start, end)\n }\n }\n\n subscribe = (listener: () => void) => {\n this.storeListeners.add(listener)\n return () => this.storeListeners.delete(listener)\n }\n\n getSnapshot = (): TapperSnapshot => {\n return this.snapshot\n }\n\n on = <K extends keyof TapperEvents>(\n event: K,\n cb: (data: TapperEvents[K]) => void,\n ) => {\n if (!this.listeners.has(event)) this.listeners.set(event, new Set())\n this.listeners.get(event)!.add(cb)\n return () => {\n this.listeners.get(event)?.delete(cb)\n }\n }\n\n private emit<K extends keyof TapperEvents>(event: K, data: TapperEvents[K]) {\n this.listeners.get(event)?.forEach(cb => cb(data))\n }\n\n private notify() {\n this.snapshot = {\n text: this.text,\n selection: this.selection,\n nodes: this.nodes,\n activeFacet: this.activeFacet,\n }\n this.storeListeners.forEach(l => l())\n }\n\n private update(newText: string, selection: TapperSelection) {\n // guard against circular updates when insert() is called during an update cycle\n if (this.updating) return\n this.updating = true\n\n const cursor = selection.end\n const isRange = selection.start !== selection.end\n\n const nodes =\n newText === this.text\n ? this.nodes\n : parseNodesFromText(\n newText,\n this.facetRegexes,\n this.nodes,\n cursor,\n this.triggers,\n )\n const prev = this.activeFacet\n const detected = isRange\n ? null\n : detectActiveFacet(nodes, newText, cursor, this.triggers)\n\n // When cursor leaves a facet, commit it\n let committedNode: TapperNode | null = null\n if (prev && (!detected || detected.type !== prev.type)) {\n for (const node of nodes) {\n if (\n node.type === 'facet' &&\n node.facetType === prev.type &&\n node.start === prev.range.start &&\n node.end === prev.range.end\n ) {\n node.committed = true\n committedNode = node\n break\n }\n }\n }\n\n // Build activeFacet with insert() baked in\n const active: TapperActiveFacet | null = detected\n ? {...detected, replace: this.replace}\n : null\n\n this.text = newText\n this.selection = selection\n this.nodes = nodes\n this.activeFacet = active\n\n this.updating = false\n this.notify()\n\n // Fire events after state is finalized\n const facetChanged =\n active?.type !== prev?.type ||\n active?.value !== prev?.value ||\n active?.range.start !== prev?.range.start ||\n active?.range.end !== prev?.range.end\n if (facetChanged) {\n this.emit('activeFacet', active)\n }\n if (committedNode) {\n this.emit('facetCommitted', nodeToFacet(committedNode))\n }\n }\n\n handleTextChange = (newText: string) => {\n if (newText === this.text) return\n\n const diff = newText.length - this.text.length\n let newEnd = Math.max(this.selection.end + diff, 0)\n\n // Atomic deletion: backspace into a facet from outside\n let didAtomicDelete = false\n if (diff === -1 && newEnd < this.selection.end) {\n for (const node of this.nodes) {\n if (\n node.type === 'facet' &&\n node.committed &&\n this.selection.end === node.end\n ) {\n const remnant = node.end - node.start - 1\n newText =\n newText.slice(0, node.start) + newText.slice(node.start + remnant)\n newEnd = node.start\n didAtomicDelete = true\n break\n }\n }\n }\n\n this.update(newText, {start: newEnd, end: newEnd})\n if (didAtomicDelete) {\n this.setInputSelection(newEnd, newEnd)\n }\n }\n\n handleSelectionChange = (e: {\n nativeEvent: {selection: {start: number; end: number}}\n }) => {\n const {start, end} = e.nativeEvent.selection\n\n if (end > this.text.length) return\n\n if (start === this.selection.start && end === this.selection.end) return\n\n this.selection = {start, end}\n\n /*\n * For selection-only changes (no text change), check if the active facet\n * would change. If not, skip the full update() cycle — just update the\n * selection in the snapshot.\n */\n const isRange = start !== end\n const detected = isRange\n ? null\n : detectActiveFacet(this.nodes, this.text, end, this.triggers)\n const prev = this.activeFacet\n const facetChanged =\n detected?.type !== prev?.type ||\n detected?.value !== prev?.value ||\n detected?.range.start !== prev?.range.start ||\n detected?.range.end !== prev?.range.end\n if (!facetChanged) {\n this.snapshot = {...this.snapshot, selection: this.selection}\n this.storeListeners.forEach(l => l())\n return\n }\n\n this.update(this.text, {start, end})\n }\n\n replaceText = (text: string, cursor?: number) => {\n const prevActive = this.activeFacet\n const nodes = parseNodesFromText(text, this.facetRegexes)\n // Mark all non-text nodes as committed (pre-existing facets)\n const newlyCommitted: TapperNode[] = []\n for (const node of nodes) {\n if (node.type === 'facet') {\n node.committed = true\n newlyCommitted.push(node)\n }\n }\n\n const pos = cursor ?? text.length\n const detected = detectActiveFacet(nodes, text, pos, this.triggers)\n const active: TapperActiveFacet | null = detected\n ? {...detected, replace: this.replace}\n : null\n\n this.text = text\n this.selection = {start: pos, end: pos}\n this.nodes = nodes\n this.activeFacet = active\n\n this.notify()\n\n if (active !== prevActive) {\n this.emit('activeFacet', active)\n }\n for (const node of newlyCommitted) {\n this.emit('facetCommitted', nodeToFacet(node))\n }\n }\n\n private replace = (value: string, options?: {noTrailingSpace?: boolean}) => {\n if (!this.activeFacet) return\n\n const replacement = value + (options?.noTrailingSpace ? '' : ' ')\n const newText =\n this.text.slice(0, this.activeFacet.range.start) +\n replacement +\n this.text.slice(this.activeFacet.range.end)\n const newEnd = this.activeFacet.range.start + replacement.length\n\n if (this.activeFacet) {\n this.activeFacet.raw = value\n this.activeFacet.value = value\n this.activeFacet.range = {\n start: this.activeFacet.range.start,\n end: this.activeFacet.range.start + value.length,\n }\n this.emit('afterInsert', this.activeFacet)\n }\n\n this.update(newText, {start: newEnd, end: newEnd})\n this.setInputSelection(newEnd, newEnd)\n }\n\n insert = (text: string) => {\n const pos = this.selection.end\n const newText = this.text.slice(0, pos) + text + this.text.slice(pos)\n const newEnd = pos + text.length\n this.update(newText, {start: newEnd, end: newEnd})\n this.setInputSelection(newEnd, newEnd)\n }\n}\n\nexport function useTapper(config: TapperConfig) {\n const [store] = useState(() => new Tapper(config))\n const state = useSyncExternalStore(store.subscribe, store.getSnapshot)\n const [input, setInput] = useState<TextInput | null>(null)\n const inputRef = useRef<{focus(): void}>(null)\n\n const handleSetInput = useCallback(\n (node: TextInput | null) => {\n inputRef.current = node\n store.setInputRef(node)\n setInput(node)\n },\n [store],\n )\n const focus = useCallback(() => input?.focus(), [input])\n const blur = useCallback(() => input?.blur(), [input])\n\n return {\n state,\n on: store.on,\n insert: store.insert,\n input: {\n element: input,\n focus,\n blur,\n },\n inputProps: {\n ref: handleSetInput,\n value: IS_WEB ? state.text : undefined,\n onChangeText: store.handleTextChange,\n onSelectionChange: store.handleSelectionChange,\n },\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsky.app/tapper",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "license": "MIT",
5
5
  "description": "A minimal rich text editor for React Native and web.",
6
6
  "repository": "https://github.com/bluesky-social/toolbox",
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {useCallback, useRef, useState, useSyncExternalStore} from 'react'
2
- import {TextInput} from 'react-native'
2
+ import {Platform, TextInput} from 'react-native'
3
3
 
4
4
  import {
5
5
  TapperConfig,
@@ -22,6 +22,8 @@ import * as defaultFacets from './facets'
22
22
  export * from './types'
23
23
  export * as facets from './facets'
24
24
 
25
+ const IS_WEB = Platform.OS === 'web'
26
+
25
27
  export class Tapper {
26
28
  private facetRegexes: CompiledFacetRegexes
27
29
  private triggers: Map<string, string>
@@ -36,31 +38,7 @@ export class Tapper {
36
38
  nodes: TapperNode[] = []
37
39
  activeFacet: TapperActiveFacet | null = null
38
40
 
39
- /*
40
- * Guard against stale native events after programmatic text replacement.
41
- *
42
- * After insert() or atomic deletion, we update this.text and this.selection
43
- * synchronously. However, iOS (especially on the old architecture bridge)
44
- * may then fire stale onChangeText and onSelectionChange events reflecting
45
- * the *previous* text/cursor state. These arrive within ~60ms.
46
- *
47
- * We record the expected textLength and a timestamp, then use two guards:
48
- *
49
- * 1. handleTextChange: if the incoming text length doesn't match the
50
- * expected length AND we're within the debounce window, it's a stale
51
- * event echoing the pre-replacement text. Skip it.
52
- *
53
- * 2. handleSelectionChange: if we're within the debounce window, skip
54
- * ALL selection events. The correct selection was already set by
55
- * update(), and any events within the window are either duplicates
56
- * (no-ops) or stale positions from the old text.
57
- *
58
- * After the window passes, the guard clears itself and normal event
59
- * handling resumes (cursor taps, typing, etc).
60
- */
61
- private pendingMutation: {textLength: number; timestamp: number} | null = null
62
- private static MUTATION_DEBOUNCE_MS = 200
63
-
41
+ private inputRef: TextInput | null = null
64
42
  private updating = false
65
43
 
66
44
  // useSyncExternalStore plumbing
@@ -82,6 +60,20 @@ export class Tapper {
82
60
  }
83
61
  }
84
62
 
63
+ setInputRef = (node: TextInput | null) => {
64
+ this.inputRef = node
65
+ }
66
+
67
+ private setInputSelection(start: number, end: number) {
68
+ const el = this.inputRef as any
69
+ if (!el) return
70
+ if (typeof el.setSelection === 'function') {
71
+ el.setSelection(start, end)
72
+ } else if (typeof el.setSelectionRange === 'function') {
73
+ el.setSelectionRange(start, end)
74
+ }
75
+ }
76
+
85
77
  subscribe = (listener: () => void) => {
86
78
  this.storeListeners.add(listener)
87
79
  return () => this.storeListeners.delete(listener)
@@ -186,26 +178,11 @@ export class Tapper {
186
178
  handleTextChange = (newText: string) => {
187
179
  if (newText === this.text) return
188
180
 
189
- /*
190
- * Guard 1: stale text. After insert/atomic deletion, iOS may echo
191
- * onChangeText with the pre-replacement text. If the incoming length
192
- * doesn't match what we expect and we're within the debounce window,
193
- * this is a stale echo — skip it.
194
- */
195
- if (this.pendingMutation !== null) {
196
- const elapsed = Date.now() - this.pendingMutation.timestamp
197
- if (
198
- elapsed < Tapper.MUTATION_DEBOUNCE_MS &&
199
- newText.length !== this.pendingMutation.textLength
200
- ) {
201
- return
202
- }
203
- }
204
-
205
181
  const diff = newText.length - this.text.length
206
182
  let newEnd = Math.max(this.selection.end + diff, 0)
207
183
 
208
184
  // Atomic deletion: backspace into a facet from outside
185
+ let didAtomicDelete = false
209
186
  if (diff === -1 && newEnd < this.selection.end) {
210
187
  for (const node of this.nodes) {
211
188
  if (
@@ -217,16 +194,16 @@ export class Tapper {
217
194
  newText =
218
195
  newText.slice(0, node.start) + newText.slice(node.start + remnant)
219
196
  newEnd = node.start
220
- this.pendingMutation = {
221
- textLength: newText.length,
222
- timestamp: Date.now(),
223
- }
197
+ didAtomicDelete = true
224
198
  break
225
199
  }
226
200
  }
227
201
  }
228
202
 
229
203
  this.update(newText, {start: newEnd, end: newEnd})
204
+ if (didAtomicDelete) {
205
+ this.setInputSelection(newEnd, newEnd)
206
+ }
230
207
  }
231
208
 
232
209
  handleSelectionChange = (e: {
@@ -234,25 +211,6 @@ export class Tapper {
234
211
  }) => {
235
212
  const {start, end} = e.nativeEvent.selection
236
213
 
237
- /*
238
- * Guard 2: stale selection. After insert/atomic deletion, iOS may fire
239
- * onSelectionChange with cursor positions from the old text state
240
- * (e.g. a "deselection" event at the pre-insert cursor). Within the
241
- * debounce window, skip ALL selection events — the correct selection
242
- * was already set synchronously by update().
243
- */
244
- if (this.pendingMutation !== null) {
245
- const elapsed = Date.now() - this.pendingMutation.timestamp
246
- if (elapsed < Tapper.MUTATION_DEBOUNCE_MS) return
247
- this.pendingMutation = null
248
- }
249
-
250
- /*
251
- * Guard 3: premature selection. iOS sometimes fires onSelectionChange
252
- * before onChangeText for the same edit, reporting a cursor position
253
- * that's beyond the current text length. Skip it — handleTextChange
254
- * will set the correct position when it arrives.
255
- */
256
214
  if (end > this.text.length) return
257
215
 
258
216
  if (start === this.selection.start && end === this.selection.end) return
@@ -336,22 +294,16 @@ export class Tapper {
336
294
  this.emit('afterInsert', this.activeFacet)
337
295
  }
338
296
 
339
- this.pendingMutation = {
340
- textLength: newText.length,
341
- timestamp: Date.now(),
342
- }
343
297
  this.update(newText, {start: newEnd, end: newEnd})
298
+ this.setInputSelection(newEnd, newEnd)
344
299
  }
345
300
 
346
301
  insert = (text: string) => {
347
302
  const pos = this.selection.end
348
303
  const newText = this.text.slice(0, pos) + text + this.text.slice(pos)
349
304
  const newEnd = pos + text.length
350
- this.pendingMutation = {
351
- textLength: newText.length,
352
- timestamp: Date.now(),
353
- }
354
305
  this.update(newText, {start: newEnd, end: newEnd})
306
+ this.setInputSelection(newEnd, newEnd)
355
307
  }
356
308
  }
357
309
 
@@ -361,10 +313,14 @@ export function useTapper(config: TapperConfig) {
361
313
  const [input, setInput] = useState<TextInput | null>(null)
362
314
  const inputRef = useRef<{focus(): void}>(null)
363
315
 
364
- const handleSetInput = useCallback((node: TextInput | null) => {
365
- inputRef.current = node
366
- setInput(node)
367
- }, [])
316
+ const handleSetInput = useCallback(
317
+ (node: TextInput | null) => {
318
+ inputRef.current = node
319
+ store.setInputRef(node)
320
+ setInput(node)
321
+ },
322
+ [store],
323
+ )
368
324
  const focus = useCallback(() => input?.focus(), [input])
369
325
  const blur = useCallback(() => input?.blur(), [input])
370
326
 
@@ -379,8 +335,7 @@ export function useTapper(config: TapperConfig) {
379
335
  },
380
336
  inputProps: {
381
337
  ref: handleSetInput,
382
- value: state.text,
383
- selection: state.selection,
338
+ value: IS_WEB ? state.text : undefined,
384
339
  onChangeText: store.handleTextChange,
385
340
  onSelectionChange: store.handleSelectionChange,
386
341
  },