@woodger/antd 0.1.3 → 0.1.4

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.
@@ -0,0 +1,48 @@
1
+ import { initializeTextAreaValue } from './textarea.dom';
2
+ /** Creates a native value connection for a textarea whose owner manages the model. */
3
+ export function createNativeTextareaValue(readValue, readDefault, trackDraftUpdates) {
4
+ const isClient = typeof document !== 'undefined';
5
+ /** Prepares the text body once per editor and primes the live value for a distinct reset. */
6
+ function prepare() {
7
+ const value = readValue();
8
+ if (!isClient) {
9
+ // HTML drops the first line break immediately after the opening textarea tag.
10
+ const body = value.startsWith('\n') ? '\n' + value : value;
11
+ return { value, body, valueAttributes: {} };
12
+ }
13
+ const explicitDefault = readDefault();
14
+ const body = explicitDefault === undefined ? value : String(explicitDefault);
15
+ const valueAttributes = body === value ? {} : { value };
16
+ return { value, body, valueAttributes };
17
+ }
18
+ /** Synchronizes model updates and preserves a matching selection on the existing DOM node. */
19
+ function connect(element) {
20
+ let previous;
21
+ $effect(() => {
22
+ const value = readValue();
23
+ const explicitDefault = readDefault();
24
+ const reset = explicitDefault === undefined ? value : String(explicitDefault);
25
+ let retainedDraft = false;
26
+ if (!previous) {
27
+ retainedDraft = value === '' && explicitDefault === undefined && element.value !== '';
28
+ if (retainedDraft) {
29
+ // The owner supplies the focus and prop reads that end a retained hydration draft.
30
+ trackDraftUpdates();
31
+ }
32
+ else {
33
+ initializeTextAreaValue(element, value, reset);
34
+ }
35
+ }
36
+ else {
37
+ if (reset !== previous.reset) {
38
+ element.defaultValue = reset;
39
+ }
40
+ if ((value !== previous.value || previous.retainedDraft) && element.value !== value) {
41
+ element.value = value;
42
+ }
43
+ }
44
+ previous = { value, reset, retainedDraft };
45
+ });
46
+ }
47
+ return { prepare, connect };
48
+ }
@@ -1,5 +1,7 @@
1
1
  import type { TextAreaProps } from './textarea.types';
2
2
  export { inputChangeEvent as textAreaChangeEvent } from './input-event.dom';
3
+ /** Sets the actual value and form default while preserving selection for matching text. */
4
+ export declare function initializeTextAreaValue(element: HTMLTextAreaElement, value: string, reset: string): void;
3
5
  export declare function createTextAreaMeasure(element: HTMLTextAreaElement, notify: (size: {
4
6
  width: number;
5
7
  height: number;
@@ -20,6 +20,23 @@ const sizing = [
20
20
  ];
21
21
  const shadows = new WeakMap();
22
22
  export { inputChangeEvent as textAreaChangeEvent } from './input-event.dom';
23
+ /** Sets the actual value and form default while preserving selection for matching text. */
24
+ export function initializeTextAreaValue(element, value, reset) {
25
+ const unchangedEmptyValue = value === '' && element.value === value;
26
+ const updateDefault = element.defaultValue !== reset;
27
+ if (unchangedEmptyValue && !updateDefault) {
28
+ return;
29
+ }
30
+ // Setting the value first keeps the native default from overwriting the displayed text.
31
+ const direction = unchangedEmptyValue ? element.selectionDirection : undefined;
32
+ element.value = value;
33
+ if (updateDefault) {
34
+ element.defaultValue = reset;
35
+ }
36
+ if (direction !== undefined && element.selectionDirection !== direction) {
37
+ element.setSelectionRange(0, 0, direction);
38
+ }
39
+ }
23
40
  // Connects the measurement to the current DOM node and its window. Returns synchronization and
24
41
  // clearing operations.
25
42
  export function createTextAreaMeasure(element, notify) {
@@ -2,6 +2,7 @@
2
2
  // Connects the word at the caret to suggestions and mention insertion. The current editor keeps
3
3
  // keyboard control.
4
4
  import { onDestroy, tick, untrack } from 'svelte';
5
+ import type { HTMLTextareaAttributes } from 'svelte/elements';
5
6
  import RenderContent from '../RenderContent.svelte';
6
7
  import RenderEmpty from '../empty/RenderEmpty.svelte';
7
8
  import type {
@@ -15,7 +16,7 @@
15
16
  mentionPopupPlacements,
16
17
  replaceMention
17
18
  } from './mentions.engine';
18
- import { captureMentionsHydrationSelection, connectMentionsMeasure } from './mentions.dom';
19
+ import { connectMentionsMeasure } from './mentions.dom';
19
20
  import { useConfig } from '../config-provider/config-context';
20
21
  import { getMentionsPrefixCss } from './mentions-prefix.engine';
21
22
  import ThemeScopeStyle from '../theme/ThemeScopeStyle.svelte';
@@ -24,6 +25,7 @@
24
25
  import { useDesignTokenContext, useRuntimeStyles } from '../theme/theme-context.svelte';
25
26
  import { getInlineZIndex, useZIndex } from '../popup/z-index-context.svelte';
26
27
  import { inputCountInfo, inputExceedValue, resolveInputClear } from '../input/input-count.engine';
28
+ import { createNativeTextareaValue } from '../input/textarea-value.svelte';
27
29
  import { mergeComponentSemantics } from '../style/semantic-style.engine';
28
30
  import { getInlineStyleDeclarations, serializeInlineStyleLayers } from '../style/inline-style.engine';
29
31
  import { createOverlayStyle } from '../popup/overlay-style.model.svelte';
@@ -201,7 +203,6 @@
201
203
  let textareaStyle = $derived(resolveTextareaStyle(semantics.styles.root, semantics.styles.textarea));
202
204
  const layer = useZIndex('popup', () => getInlineZIndex(semantics.styles.popup));
203
205
  let controlId = $derived(id ?? field?.id ?? `ant-mentions-${generated}`);
204
- const restoreHydrationSelection = untrack(() => captureMentionsHydrationSelection(controlId));
205
206
  let prefixes = $derived(typeof prefix === 'string' ? [prefix] : prefix);
206
207
  let measuring = $state(false);
207
208
  let query = $state('');
@@ -260,6 +261,56 @@
260
261
  if (optionId !== undefined) return optionId;
261
262
  }
262
263
  });
264
+ const editorAttributes = $derived.by(() => {
265
+ const { defaultvalue: _nativeDefault, ...nativeAttributes } = attributes;
266
+ return {
267
+ ...nativeAttributes,
268
+ 'data-ant-mentions-textarea': true,
269
+ id: controlId,
270
+ name: name ?? field?.name,
271
+ rows: attributes.rows ?? 1,
272
+ maxlength: maximum,
273
+ class: [
274
+ `${componentPrefix}-textarea`,
275
+ config.direction === 'rtl' && `${componentPrefix}-textarea-rtl`,
276
+ !counterVisible && counter.exceeded && 'rc-textarea-out-of-range',
277
+ semantics.classes.textarea
278
+ ],
279
+ style: textareaStyle,
280
+ dir: attributes.dir ?? config.direction,
281
+ readonly,
282
+ disabled: blocked,
283
+ required: attributes.required ?? field?.required,
284
+ role: 'combobox',
285
+ 'aria-expanded': visible,
286
+ ...(attributes.autofocus !== undefined || autoFocus !== undefined
287
+ ? { autofocus: attributes.autofocus ?? autoFocus } : {}),
288
+ 'aria-autocomplete': 'list',
289
+ 'aria-haspopup': 'listbox',
290
+ 'aria-controls': controlledMenus,
291
+ 'aria-activedescendant': activeMenuOptionId,
292
+ 'aria-label': attributes['aria-label'] ?? field?.label,
293
+ 'aria-invalid': field?.invalid || resolvedStatus === 'error'
294
+ || attributes['aria-invalid'] || undefined,
295
+ 'aria-describedby': [attributes['aria-describedby'], field?.describedBy].filter(Boolean)
296
+ .join(' ') || undefined,
297
+ oninput: publishEditorInput,
298
+ onkeydown: handleKeydown,
299
+ onkeyup: handleKeyup,
300
+ oncompositionstart: handleCompositionStart,
301
+ oncompositionend: handleCompositionEnd,
302
+ onfocus: handleFocus,
303
+ onblur: handleBlur
304
+ } satisfies HTMLTextareaAttributes;
305
+ });
306
+ const nativeEditorValue = createNativeTextareaValue(
307
+ () => text,
308
+ () => attributes.defaultvalue,
309
+ () => {
310
+ void editorFocused;
311
+ void editorAttributes;
312
+ }
313
+ );
263
314
  const menuOwner: MentionsListboxOwner = {
264
315
  get id() { return controlId; },
265
316
  get active() { return active; },
@@ -508,11 +559,6 @@
508
559
  onChange?.(next);
509
560
  }
510
561
 
511
- function bindEditor(element: HTMLTextAreaElement | null | undefined) {
512
- if (element) restoreHydrationSelection?.(element);
513
- editor = element ?? undefined;
514
- }
515
-
516
562
  function publishEditorInput(event: EditorEvent) {
517
563
  if (blocked || readonly) {
518
564
  return;
@@ -821,51 +867,24 @@
821
867
  semantics.classes.root
822
868
  ]}>
823
869
  {#snippet editorControl()}
824
- {@const hasNativeDefault = untrack(() => count !== undefined && typeof document !== 'undefined')}
825
- <!-- RcInput sets the DOM property defaultValue before value to preserve the initial caret position. -->
826
- <textarea
827
- {...(hasNativeDefault ? { defaultValue: text } : {})}
828
- {...attributes}
829
- data-ant-mentions-textarea
830
- bind:this={() => editor, bindEditor}
831
- id={controlId}
832
- name={name ?? field?.name}
833
- rows={attributes.rows ?? 1}
834
- maxlength={maximum}
835
- class={[
836
- `${componentPrefix}-textarea`,
837
- config.direction === 'rtl' && `${componentPrefix}-textarea-rtl`,
838
- !counterVisible && counter.exceeded && 'rc-textarea-out-of-range',
839
- semantics.classes.textarea
840
- ]}
841
- style={textareaStyle}
842
- dir={attributes.dir ?? config.direction}
843
- value={text}
844
- {readonly}
845
- disabled={blocked}
846
- required={attributes.required ?? field?.required}
847
- role="combobox"
848
- aria-expanded={visible}
849
- {...(attributes.autofocus !== undefined || autoFocus !== undefined
850
- ? { autofocus: attributes.autofocus ?? autoFocus } : {})}
851
- aria-autocomplete="list"
852
- aria-haspopup="listbox"
853
- aria-controls={controlledMenus}
854
- aria-activedescendant={activeMenuOptionId}
855
- aria-label={attributes['aria-label'] ?? field?.label}
856
- aria-invalid={field?.invalid || resolvedStatus === 'error'
857
- || attributes['aria-invalid']
858
- || undefined}
859
- aria-describedby={[attributes['aria-describedby'], field?.describedBy].filter(Boolean)
860
- .join(' ') || undefined}
861
- oninput={publishEditorInput}
862
- onkeydown={handleKeydown}
863
- onkeyup={handleKeyup}
864
- oncompositionstart={handleCompositionStart}
865
- oncompositionend={handleCompositionEnd}
866
- onfocus={handleFocus}
867
- onblur={handleBlur}
868
- ></textarea>
870
+ {@const initial = untrack(nativeEditorValue.prepare)}
871
+ {#if initial.value === ''}
872
+ <!-- Appending a hydration marker to an empty textarea resets its selection direction. -->
873
+ <textarea
874
+ {...editorAttributes}
875
+ bind:this={editor}
876
+ {@attach nativeEditorValue.connect}
877
+ ></textarea>
878
+ {:else}
879
+ <!-- A literal textarea loses its SSR text before Svelte initializes its value. -->
880
+ <svelte:element
881
+ this={'textarea'}
882
+ {...editorAttributes}
883
+ {...initial.valueAttributes}
884
+ bind:this={editor}
885
+ {@attach nativeEditorValue.connect}
886
+ >{initial.body}</svelte:element>
887
+ {/if}
869
888
  {/snippet}
870
889
  {#key editorGeneration}
871
890
  {#if counterVisible}
@@ -1,6 +1,4 @@
1
1
  import type { MentionsProps } from './mentions.types';
2
- /** Excites the state before Svelte clears the textarea node during hydration. */
3
- export declare function captureMentionsHydrationSelection(id: string): ((editor: HTMLTextAreaElement) => void) | undefined;
4
2
  export declare function connectMentionsMeasure(textarea: HTMLTextAreaElement, mirror: HTMLElement, autoSize: MentionsProps['autoSize'], onResize: MentionsProps['onResize'], getEditorStyle: () => string | undefined): {
5
3
  sync: () => void;
6
4
  destroy(): void;
@@ -1,57 +1,3 @@
1
- /** Reads the state only at the native textarea Mentions. */
2
- function readEditorSelection(editor) {
3
- const Textarea = editor?.ownerDocument.defaultView?.HTMLTextAreaElement;
4
- if (!Textarea
5
- || !(editor instanceof Textarea)
6
- || !editor.hasAttribute('data-ant-mentions-textarea')) {
7
- return;
8
- }
9
- return {
10
- editor,
11
- value: editor.value,
12
- start: editor.selectionStart,
13
- end: editor.selectionEnd,
14
- direction: editor.selectionDirection
15
- };
16
- }
17
- /** Gathers candidates in a document and open trees along the focus chain. */
18
- function readHydrationSelections(id) {
19
- if (typeof document === 'undefined') {
20
- return;
21
- }
22
- const snapshots = [];
23
- let scope = document;
24
- while (scope) {
25
- const focused = scope.activeElement;
26
- const candidate = focused?.id === id ? focused : scope.getElementById(id);
27
- const snapshot = readEditorSelection(candidate);
28
- if (snapshot)
29
- snapshots.push(snapshot);
30
- scope = focused?.shadowRoot ?? undefined;
31
- }
32
- return snapshots;
33
- }
34
- /** Excites the state before Svelte clears the textarea node during hydration. */
35
- export function captureMentionsHydrationSelection(id) {
36
- let pending = readHydrationSelections(id);
37
- if (!pending?.length) {
38
- return;
39
- }
40
- /** Applies the picture once to the same editor if its text is preserved. */
41
- return function restoreSelection(editor) {
42
- const previous = pending?.find(snapshot => snapshot.editor === editor);
43
- pending = undefined;
44
- if (!previous || editor.value !== previous.value) {
45
- return;
46
- }
47
- const { start, end, direction } = previous;
48
- if (editor.selectionStart !== start
49
- || editor.selectionEnd !== end
50
- || editor.selectionDirection !== direction) {
51
- editor.setSelectionRange(start, end, direction);
52
- }
53
- };
54
- }
55
1
  // Calculation autoSize follows calculateNodeHeight from @rc-component/input 1.3.1 (MIT).
56
2
  const sizing = [
57
3
  'letter-spacing',
@@ -32,6 +32,8 @@
32
32
  let shared = $derived(time && rootTimeProps
33
33
  ? { ...timeRangeSharedProps(time), showNow: rootTimeProps.showNow }
34
34
  : dateProps && calendarOptions(dateProps));
35
+ // Updating shared callbacks must keep the owner's original value tuple in the core.
36
+ let value = $derived(props.value);
35
37
  let picker = $state<{
36
38
  focus(options?: FocusOptions & { index?: 0 | 1; cursor?: 'start' | 'end' | 'all'; }): void;
37
39
  blur(): void;
@@ -75,7 +77,7 @@
75
77
  }
76
78
  </script>
77
79
 
78
- <RangePickerCore {...shared} {dateOperations} {pickerCallbackOwner} {parseTimeInput}
80
+ <RangePickerCore {...shared} {value} {dateOperations} {pickerCallbackOwner} {parseTimeInput}
79
81
  {dateProps} {rootTimeProps}
80
82
  {time} {clockFormat}
81
83
  bind:nativeElement bind:startInput bind:endInput bind:this={picker} />