| import { |
| DEFAULT_EXCLUDE_GENERATED_PATTERNS_TEXT, |
| DEFAULT_EXCLUDE_PROMPT_PATTERNS_TEXT, |
| EXCLUDE_GENERATED_PATTERNS_ENABLED_STORAGE_KEY, |
| EXCLUDE_GENERATED_PATTERNS_STORAGE_KEY, |
| EXCLUDE_PROMPT_PATTERNS_ENABLED_STORAGE_KEY, |
| EXCLUDE_PROMPT_PATTERNS_STORAGE_KEY, |
| } from './attributionExcludePromptPatternsStorage'; |
|
|
| type BindExcludePatternsUiStorageKeys = { |
| textKey: string; |
| enabledKey: string; |
| }; |
|
|
| type BindExcludePatternsUiOptions = { |
| storageKeys: BindExcludePatternsUiStorageKeys; |
| textInput: HTMLInputElement | HTMLTextAreaElement | null; |
| enableCheckbox: HTMLInputElement | null; |
| |
| onEffectiveChange: () => void; |
| |
| defaultTextWhenKeyAbsent?: string; |
| }; |
|
|
| export type BindExcludePromptPatternsUiOptions = Omit<BindExcludePatternsUiOptions, 'storageKeys' | 'defaultTextWhenKeyAbsent'>; |
|
|
| |
| |
| |
| function bindExcludePatternsUi(options: BindExcludePatternsUiOptions): void { |
| const { storageKeys, textInput, enableCheckbox, onEffectiveChange, defaultTextWhenKeyAbsent } = options; |
| const { textKey, enabledKey } = storageKeys; |
|
|
| try { |
| const savedExclude = localStorage.getItem(textKey); |
| if (textInput) { |
| if (savedExclude !== null) { |
| textInput.value = savedExclude; |
| } else if (defaultTextWhenKeyAbsent !== undefined) { |
| textInput.value = defaultTextWhenKeyAbsent; |
| } |
| } |
| const savedEnabled = localStorage.getItem(enabledKey); |
| if (enableCheckbox) { |
| enableCheckbox.checked = savedEnabled === null ? true : savedEnabled === '1'; |
| } |
| } catch { |
| |
| } |
|
|
| function syncTextInputDisabled(): void { |
| if (!textInput) return; |
| textInput.disabled = !enableCheckbox?.checked; |
| } |
| syncTextInputDisabled(); |
|
|
| enableCheckbox?.addEventListener('change', () => { |
| try { |
| if (textInput) { |
| localStorage.setItem(textKey, textInput.value); |
| } |
| localStorage.setItem(enabledKey, enableCheckbox.checked ? '1' : '0'); |
| } catch { |
| |
| } |
| syncTextInputDisabled(); |
| onEffectiveChange(); |
| }); |
|
|
| textInput?.addEventListener('blur', () => { |
| try { |
| localStorage.setItem(textKey, textInput.value); |
| } catch { |
| |
| } |
| onEffectiveChange(); |
| }); |
|
|
| window.addEventListener('storage', (event: StorageEvent) => { |
| if (event.storageArea !== localStorage) return; |
| const k = event.key; |
| if (k !== textKey && k !== enabledKey) { |
| return; |
| } |
| if (k === textKey && textInput) textInput.value = event.newValue ?? ''; |
| if (k === enabledKey && enableCheckbox) { |
| enableCheckbox.checked = event.newValue === null ? true : event.newValue === '1'; |
| } |
| syncTextInputDisabled(); |
| onEffectiveChange(); |
| }); |
| } |
|
|
| |
| |
| |
| export function bindExcludePromptPatternsUi(options: BindExcludePromptPatternsUiOptions): void { |
| bindExcludePatternsUi({ |
| storageKeys: { |
| textKey: EXCLUDE_PROMPT_PATTERNS_STORAGE_KEY, |
| enabledKey: EXCLUDE_PROMPT_PATTERNS_ENABLED_STORAGE_KEY, |
| }, |
| ...options, |
| defaultTextWhenKeyAbsent: DEFAULT_EXCLUDE_PROMPT_PATTERNS_TEXT, |
| }); |
| } |
|
|
| |
| export function bindExcludeGeneratedPatternsUi(options: BindExcludePromptPatternsUiOptions): void { |
| bindExcludePatternsUi({ |
| storageKeys: { |
| textKey: EXCLUDE_GENERATED_PATTERNS_STORAGE_KEY, |
| enabledKey: EXCLUDE_GENERATED_PATTERNS_ENABLED_STORAGE_KEY, |
| }, |
| ...options, |
| defaultTextWhenKeyAbsent: DEFAULT_EXCLUDE_GENERATED_PATTERNS_TEXT, |
| }); |
| } |
|
|