@sparkvault/sdk-js 18.0.1 → 18.0.2

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
@@ -4620,6 +4620,220 @@ class LoadingView {
4620
4620
  }
4621
4621
  }
4622
4622
 
4623
+ /**
4624
+ * Shared browser email-or-phone field. Entered text is held separately from
4625
+ * phone presentation, so a numeric email prefix never inherits punctuation
4626
+ * inserted by the formatter. Callers validate `value`, never the presentation.
4627
+ */
4628
+
4629
+ const PHONE_CHARACTER = /[+\d]/;
4630
+
4631
+ /** Map each displayed phone digit to the entered character it represents. */
4632
+ function phonePositions(value, display) {
4633
+ const entered = [...value.matchAll(/[+\d]/g)].map((match) => match.index);
4634
+ const shown = [...display.matchAll(/[+\d]/g)].map((match) => match.index);
4635
+ // NANP presentation omits its declared country code; it stays in the value.
4636
+ return shown.map((position, index) => ({ display: position, value: entered[index + entered.length - shown.length] }));
4637
+ }
4638
+
4639
+ /**
4640
+ * @param {HTMLInputElement} input - A text or tel input owned by the caller
4641
+ * @param {{allowedTypes?: ('email'|'phone')[], onChange?: (value: string, type: 'email'|'phone') => void}} options
4642
+ * @returns {{readonly value: string, readonly type: 'email'|'phone', setValue: (value: string) => void, destroy: () => void}}
4643
+ */
4644
+ function attachIdentityInput(input, { allowedTypes = ['email', 'phone'], onChange } = {}) {
4645
+ let value = input.value;
4646
+ let display = value;
4647
+ let positions = [];
4648
+ let composing = false;
4649
+ let beforeEdit = null;
4650
+ let history = [{ value, start: value.length, end: value.length }];
4651
+ let historyIndex = 0;
4652
+ const phoneOnly = allowedTypes.length === 1 && allowedTypes[0] === 'phone';
4653
+ const allowsPhone = allowedTypes.includes('phone');
4654
+ const identityType = () => phoneOnly ? 'phone' : allowsPhone ? inferIdentityType(value) : 'email';
4655
+
4656
+ function valuePosition(position) {
4657
+ if (display === value || position === 0) return position;
4658
+ if (position >= display.length) return value.length;
4659
+ return positions.find((entry) => entry.display >= position)?.value ?? value.length;
4660
+ }
4661
+
4662
+ function displayPosition(position) {
4663
+ if (display === value) return position;
4664
+ if (position >= value.length) return display.length;
4665
+ for (let index = positions.length - 1; index >= 0; index -= 1) {
4666
+ if (positions[index].value < position) return positions[index].display + 1;
4667
+ }
4668
+ return positions[0]?.display ?? 0;
4669
+ }
4670
+
4671
+ function present(start = null, end = start) {
4672
+ display = identityType() === 'phone' ? formatPhoneDisplay(value) : value;
4673
+ positions = display === value ? [] : phonePositions(value, display);
4674
+ input.value = display;
4675
+ // Mixed fields keep a text keyboard: an initial digit can begin an email.
4676
+ input.inputMode = phoneOnly ? 'tel' : 'text';
4677
+ input.autocapitalize = 'none';
4678
+ input.spellcheck = false;
4679
+ if (start !== null) input.setSelectionRange(displayPosition(start), displayPosition(end));
4680
+ }
4681
+
4682
+ // Formatting writes the DOM value, which changes native undo bookkeeping.
4683
+ // History therefore owns entered values and selections, never DOM diffs.
4684
+ function restoreHistory(direction) {
4685
+ beforeEdit = null;
4686
+ const nextIndex = Math.max(0, Math.min(history.length - 1, historyIndex + direction));
4687
+ const changed = nextIndex !== historyIndex;
4688
+ historyIndex = nextIndex;
4689
+ const state = history[historyIndex];
4690
+ value = state.value;
4691
+ present();
4692
+ input.setSelectionRange(state.start, state.end);
4693
+ if (changed) onChange?.(value, identityType());
4694
+ }
4695
+
4696
+ function historyDirection(event) {
4697
+ return event.inputType === 'historyUndo' ? -1 : event.inputType === 'historyRedo' ? 1 : 0;
4698
+ }
4699
+
4700
+ function handleHistoryKey(event) {
4701
+ if (composing || event.isComposing || event.altKey || !(event.metaKey || event.ctrlKey)) return;
4702
+ const key = event.key.toLowerCase();
4703
+ const direction = key === 'z' ? (event.shiftKey ? 1 : -1) : key === 'y' && event.ctrlKey ? 1 : 0;
4704
+ if (!direction) return;
4705
+ event.preventDefault();
4706
+ restoreHistory(direction);
4707
+ }
4708
+
4709
+ function rememberSelection() {
4710
+ const start = input.selectionStart ?? display.length;
4711
+ const end = input.selectionEnd ?? start;
4712
+ history[historyIndex] = { value, start, end };
4713
+ }
4714
+
4715
+ function captureEdit(event) {
4716
+ if (composing || event.isComposing) return;
4717
+ const direction = historyDirection(event);
4718
+ if (direction) {
4719
+ if (event.cancelable) {
4720
+ event.preventDefault();
4721
+ restoreHistory(direction);
4722
+ }
4723
+ return;
4724
+ }
4725
+ rememberSelection();
4726
+ let start = input.selectionStart ?? display.length;
4727
+ let end = input.selectionEnd ?? start;
4728
+ if (start === end && event.inputType === 'deleteContentBackward') start = Math.max(0, start - 1);
4729
+ if (start === end && event.inputType === 'deleteContentForward') end = Math.min(display.length, end + 1);
4730
+ beforeEdit = { start, end, inputType: event.inputType, display };
4731
+ }
4732
+
4733
+ function acceptEdit(event) {
4734
+ if (composing || event.isComposing) return;
4735
+ const direction = historyDirection(event);
4736
+ if (direction) {
4737
+ restoreHistory(direction);
4738
+ return;
4739
+ }
4740
+ const next = input.value;
4741
+ if (next === display) {
4742
+ beforeEdit = null;
4743
+ return;
4744
+ }
4745
+ const cursor = input.selectionStart ?? next.length;
4746
+ const selectionEnd = input.selectionEnd ?? cursor;
4747
+ let start = 0;
4748
+ let end = display.length;
4749
+ let nextEnd = next.length;
4750
+ const edit = beforeEdit;
4751
+ beforeEdit = null;
4752
+ if (edit?.display === display && next.startsWith(display.slice(0, edit.start))
4753
+ && next.endsWith(display.slice(edit.end))) {
4754
+ start = edit.start;
4755
+ end = edit.end;
4756
+ nextEnd = next.length - (display.length - end);
4757
+ } else {
4758
+ while (start < display.length && start < next.length && display[start] === next[start]) start += 1;
4759
+ while (end > start && nextEnd > start && display[end - 1] === next[nextEnd - 1]) {
4760
+ end -= 1;
4761
+ nextEnd -= 1;
4762
+ }
4763
+ }
4764
+ let rawStart = valuePosition(start);
4765
+ let rawEnd = valuePosition(end);
4766
+ const inserted = next.slice(start, nextEnd);
4767
+ // Backspace/Delete on presentation punctuation must make progress instead
4768
+ // of immediately restoring the separator and trapping the caret.
4769
+ if (!inserted && end > start && rawStart === rawEnd) {
4770
+ const forward = (edit?.inputType || event.inputType) === 'deleteContentForward';
4771
+ if (forward) {
4772
+ while (rawEnd < value.length && !PHONE_CHARACTER.test(value[rawEnd])) rawEnd += 1;
4773
+ rawEnd = Math.min(value.length, rawEnd + 1);
4774
+ } else {
4775
+ while (rawStart > 0 && !PHONE_CHARACTER.test(value[rawStart - 1])) rawStart -= 1;
4776
+ rawStart = Math.max(0, rawStart - 1);
4777
+ }
4778
+ }
4779
+ const toValuePosition = (position) => position < start
4780
+ ? valuePosition(position)
4781
+ : position <= nextEnd
4782
+ ? rawStart + position - start
4783
+ : valuePosition(position - nextEnd + end) + inserted.length - (rawEnd - rawStart);
4784
+ const rawCursor = toValuePosition(cursor);
4785
+ const rawSelectionEnd = toValuePosition(selectionEnd);
4786
+ value = value.slice(0, rawStart) + inserted + value.slice(rawEnd);
4787
+ present(rawCursor, rawSelectionEnd);
4788
+ history.splice(historyIndex + 1);
4789
+ history.push({ value, start: input.selectionStart, end: input.selectionEnd });
4790
+ historyIndex = history.length - 1;
4791
+ onChange?.(value, identityType());
4792
+ }
4793
+
4794
+ function startComposition() {
4795
+ rememberSelection();
4796
+ composing = true;
4797
+ beforeEdit = null;
4798
+ }
4799
+
4800
+ function endComposition(event) {
4801
+ composing = false;
4802
+ acceptEdit(event);
4803
+ }
4804
+
4805
+ input.addEventListener('keydown', handleHistoryKey);
4806
+ input.addEventListener('beforeinput', captureEdit);
4807
+ input.addEventListener('input', acceptEdit);
4808
+ input.addEventListener('change', acceptEdit);
4809
+ input.addEventListener('compositionstart', startComposition);
4810
+ input.addEventListener('compositionend', endComposition);
4811
+ present();
4812
+ rememberSelection();
4813
+
4814
+ return {
4815
+ get value() { return value; },
4816
+ get type() { return identityType(); },
4817
+ setValue(next) {
4818
+ if (next === value) return;
4819
+ value = next;
4820
+ history = [{ value, start: value.length, end: value.length }];
4821
+ historyIndex = 0;
4822
+ beforeEdit = null;
4823
+ present();
4824
+ rememberSelection();
4825
+ },
4826
+ destroy() {
4827
+ input.removeEventListener('keydown', handleHistoryKey);
4828
+ input.removeEventListener('beforeinput', captureEdit);
4829
+ input.removeEventListener('input', acceptEdit);
4830
+ input.removeEventListener('change', acceptEdit);
4831
+ input.removeEventListener('compositionstart', startComposition);
4832
+ input.removeEventListener('compositionend', endComposition);
4833
+ }
4834
+ };
4835
+ }
4836
+
4623
4837
  /**
4624
4838
  * The sign-in providers' marks and the social networks' marks, each stated
4625
4839
  * once: the identity dialog (the browser SDK, which bundles this module) and
@@ -5115,7 +5329,7 @@ function smsConsent(id) {
5115
5329
  *
5116
5330
  * Collects and validates the user's email or phone number: reads the input as
5117
5331
  * one or the other by the platform's rules (`@sparkvault/app-sdk/phone` and
5118
- * `@sparkvault/app-sdk/email-address`), and formats a phone-only field as the
5332
+ * `@sparkvault/app-sdk/email-address`), and formats phone numbers as the
5119
5333
  * number is typed. A field that takes a phone number carries the SMS and voice
5120
5334
  * consent beneath it.
5121
5335
  */
@@ -5130,12 +5344,8 @@ class IdentityInputView {
5130
5344
  this.leadingHandler = null;
5131
5345
  this.errorContainer = null;
5132
5346
  this.focusTimeoutId = null;
5133
- this.detectedType = 'email';
5347
+ this.identityInput = null;
5134
5348
  this.props = props;
5135
- this.boundHandleInput = () => {
5136
- this.handleInput();
5137
- this.props.onType?.();
5138
- };
5139
5349
  this.boundHandleKeyDown = (e) => {
5140
5350
  if (e.key === 'Enter') {
5141
5351
  e.preventDefault();
@@ -5190,9 +5400,17 @@ class IdentityInputView {
5190
5400
  this.inputElement.maxLength = EMAIL_ADDRESS_LIMITS.ADDRESS;
5191
5401
  if (this.props.initialValue) {
5192
5402
  this.inputElement.value = this.props.initialValue;
5193
- this.detectAndUpdateType(this.props.initialValue);
5194
5403
  }
5195
- this.inputElement.addEventListener('input', this.boundHandleInput);
5404
+ this.identityInput = attachIdentityInput(this.inputElement, {
5405
+ allowedTypes: this.props.allowedTypes,
5406
+ onChange: () => {
5407
+ this.clearError();
5408
+ if (this.labelElement)
5409
+ this.labelElement.textContent = this.getLabelText();
5410
+ this.props.onType?.();
5411
+ }
5412
+ });
5413
+ this.labelElement.textContent = this.getLabelText();
5196
5414
  this.inputElement.addEventListener('keydown', this.boundHandleKeyDown);
5197
5415
  inputWrapper.appendChild(this.labelElement);
5198
5416
  inputWrapper.appendChild(this.inputElement);
@@ -5245,7 +5463,7 @@ class IdentityInputView {
5245
5463
  }
5246
5464
  getLabelText() {
5247
5465
  if (this.allowsBoth) {
5248
- return this.detectedType === 'phone' ? 'Phone number' : 'Email address';
5466
+ return this.identityInput?.type === 'phone' ? 'Phone number' : 'Email address';
5249
5467
  }
5250
5468
  if (this.allowsPhone)
5251
5469
  return 'Phone number';
@@ -5258,36 +5476,6 @@ class IdentityInputView {
5258
5476
  return '(555) 123-4567';
5259
5477
  return 'you@example.com';
5260
5478
  }
5261
- handleInput() {
5262
- this.clearError();
5263
- if (!this.inputElement)
5264
- return;
5265
- const value = this.inputElement.value;
5266
- if (this.allowsBoth) {
5267
- // The label follows what the input reads as, but the text is never
5268
- // rewritten: digits typed so far can still begin an email address
5269
- // ("2024user@gmail.com", "12345678@qq.com"), and phone formatting would
5270
- // corrupt it. Submit normalizes a phone number whatever its separators.
5271
- const wasPhone = this.detectedType === 'phone';
5272
- this.detectAndUpdateType(value);
5273
- if (wasPhone !== (this.detectedType === 'phone') && this.labelElement) {
5274
- this.labelElement.textContent = this.getLabelText();
5275
- }
5276
- }
5277
- else if (this.allowsPhone) {
5278
- // Phone-only mode: always format
5279
- const cursorPos = this.inputElement.selectionStart ?? value.length;
5280
- const formatted = formatPhoneDisplay(value);
5281
- if (formatted !== value) {
5282
- this.inputElement.value = formatted;
5283
- const newPos = Math.min(cursorPos + (formatted.length - value.length), formatted.length);
5284
- this.inputElement.setSelectionRange(newPos, newPos);
5285
- }
5286
- }
5287
- }
5288
- detectAndUpdateType(value) {
5289
- this.detectedType = inferIdentityType(value);
5290
- }
5291
5479
  clearError() {
5292
5480
  if (this.errorContainer) {
5293
5481
  clearChildren(this.errorContainer);
@@ -5303,60 +5491,27 @@ class IdentityInputView {
5303
5491
  this.inputElement?.focus();
5304
5492
  }
5305
5493
  handleSubmit() {
5306
- if (!this.inputElement || !this.submitButton)
5494
+ if (!this.identityInput || !this.submitButton)
5495
+ return;
5496
+ const type = this.identityInput.type;
5497
+ const value = type === 'phone'
5498
+ ? parsePhone(this.identityInput.value)
5499
+ : validateEmail(this.identityInput.value);
5500
+ if (!value) {
5501
+ this.showError(type === 'phone' ? 'Please enter a valid phone number' : 'Please enter a valid email address');
5307
5502
  return;
5308
- const rawValue = this.inputElement.value;
5309
- // Validate based on detected type
5310
- if (this.allowsBoth) {
5311
- if (this.detectedType === 'phone') {
5312
- const validPhone = parsePhone(rawValue);
5313
- if (!validPhone) {
5314
- this.showError('Please enter a valid phone number');
5315
- return;
5316
- }
5317
- this.submitButton.disabled = true;
5318
- this.submitButton.textContent = 'Please wait...';
5319
- this.props.onSubmit({ value: validPhone, type: 'phone' });
5320
- }
5321
- else {
5322
- const validEmail = validateEmail(rawValue);
5323
- if (!validEmail) {
5324
- this.showError('Please enter a valid email address');
5325
- return;
5326
- }
5327
- this.submitButton.disabled = true;
5328
- this.submitButton.textContent = 'Please wait...';
5329
- this.props.onSubmit({ value: validEmail, type: 'email' });
5330
- }
5331
- }
5332
- else if (this.allowsPhone) {
5333
- const validPhone = parsePhone(rawValue);
5334
- if (!validPhone) {
5335
- this.showError('Please enter a valid phone number');
5336
- return;
5337
- }
5338
- this.submitButton.disabled = true;
5339
- this.submitButton.textContent = 'Please wait...';
5340
- this.props.onSubmit({ value: validPhone, type: 'phone' });
5341
- }
5342
- else {
5343
- const validEmail = validateEmail(rawValue);
5344
- if (!validEmail) {
5345
- this.showError('Please enter a valid email address');
5346
- return;
5347
- }
5348
- this.submitButton.disabled = true;
5349
- this.submitButton.textContent = 'Please wait...';
5350
- this.props.onSubmit({ value: validEmail, type: 'email' });
5351
5503
  }
5504
+ this.submitButton.disabled = true;
5505
+ this.submitButton.textContent = 'Please wait...';
5506
+ this.props.onSubmit({ value, type });
5352
5507
  }
5353
5508
  destroy() {
5509
+ this.identityInput?.destroy();
5354
5510
  if (this.focusTimeoutId !== null) {
5355
5511
  clearTimeout(this.focusTimeoutId);
5356
5512
  this.focusTimeoutId = null;
5357
5513
  }
5358
5514
  if (this.inputElement) {
5359
- this.inputElement.removeEventListener('input', this.boundHandleInput);
5360
5515
  this.inputElement.removeEventListener('keydown', this.boundHandleKeyDown);
5361
5516
  }
5362
5517
  if (this.submitButton) {
@@ -31664,7 +31819,8 @@ class SparksModule {
31664
31819
  /**
31665
31820
  * Deliver an invite-only spark's link to its invitee again, changing
31666
31821
  * nothing about the link: the way on from an invitation that could not be
31667
- * delivered (a share rejected with INVITATION_DELIVERY_FAILED leaves its
31822
+ * delivered (a share rejected with INVITATION_DELIVERY_FAILED or a 402
31823
+ * usage-credit funding refusal leaves its
31668
31824
  * link in place, and the same share again delivers nothing), or one the
31669
31825
  * invitee never received. `linkCode` names the link the resend was asked
31670
31826
  * of: a link replaced since is never delivered in its place (409 CONFLICT).
@@ -32292,7 +32448,7 @@ class SparkVault {
32292
32448
  }
32293
32449
  /** Get the SDK version */
32294
32450
  static get version() {
32295
- return '18.0.1';
32451
+ return '18.0.2';
32296
32452
  }
32297
32453
  }
32298
32454
  if (typeof window !== 'undefined') {