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