The swiss army knife of lossless video/audio editing
Save template edits with a flushable debounce
Claude0c510cd46112
diff --git a/src/renderer/src/components/FileNameTemplateEditor.tsx b/src/renderer/src/components/FileNameTemplateEditor.tsxindex 016f5a33..cd95e2ca 100644--- a/src/renderer/src/components/FileNameTemplateEditor.tsx+++ b/src/renderer/src/components/FileNameTemplateEditor.tsx@@ -1,6 +1,6 @@import type { ChangeEventHandler } from 'react';import { memo, useState, useEffect, useCallback, useRef, useMemo } from 'react';-import { useDebounce } from 'use-debounce';+import { useDebounce, useDebouncedCallback } from 'use-debounce';import { useTranslation } from 'react-i18next';import { IoIosHelpCircle } from 'react-icons/io';import { motion, AnimatePresence } from 'motion/react';@@ -43,6 +43,8 @@ function FileNameTemplateEditor(opts: {const [text, setText] = useState(templateIn);const [debouncedText] = useDebounce(text, 500);+ // save the template from the change events (not from debouncedText), so that it can be flushed, and so that it always follows the text (also after an undo)+ const debouncedSetTemplate = useDebouncedCallback(setTemplate, 500, { flushOnExit: true });const [generated, setGenerated] = useState<GeneratedOutFileNames>();const isSimpleMergeFilesMode = simpleMode && mode === 'merge-files';@@ -116,31 +118,36 @@ function FileNameTemplateEditor(opts: {const isMissingExtension = !debouncedText.endsWith(extVariableFormatted);- useEffect(() => {- setTemplate(debouncedText);- }, [debouncedText, setTemplate]);-useEffect(() => {if (open) inputRef.current?.focus();}, [open]);const reset = useCallback(() => {- // edit through the input so that native undo works. This goes through onChange and the debounce, so that the saved template always follows the text (also after an undo)+ // edit through the input so that native undo worksconst input = inputRef.current;if (input != null) {input.focus();input.select();- if (document.execCommand('insertText', false, defaultTemplate)) return;+ if (document.execCommand('insertText', false, defaultTemplate)) {+ debouncedSetTemplate.flush(); // save right away+ return;+ }}+ debouncedSetTemplate.cancel();setTemplate(defaultTemplate);setText(defaultTemplate);- }, [defaultTemplate, setTemplate]);+ }, [debouncedSetTemplate, defaultTemplate, setTemplate]);const handleSampleClick = useCallback(() => {setOpen((v) => !v);}, []);- const onTextChange = useCallback<ChangeEventHandler<HTMLInputElement>>((e) => setText(e.target.value), []);+ const onTextChange = useCallback<ChangeEventHandler<HTMLInputElement>>((e) => {+ setText(e.target.value);+ debouncedSetTemplate(e.target.value);+ }, [debouncedSetTemplate]);++ const onTextBlur = useCallback(() => debouncedSetTemplate.flush(), [debouncedSetTemplate]);const onVariableClick = useCallback((variable: string) => {const input = inputRef.current;@@ -157,7 +164,8 @@ function FileNameTemplateEditor(opts: {const newValue = `${text.slice(0, startPos)}${`${formatVariable(toInsert)}${text.slice(endPos)}`}`;setText(newValue);- }, [text]);+ debouncedSetTemplate(newValue);+ }, [debouncedSetTemplate, text]);function formatCurrentSegFileOrFirst(names: string[]) {if (mode === 'separate') {@@ -213,7 +221,7 @@ function FileNameTemplateEditor(opts: {)}<div style={{ display: 'flex', alignItems: 'center', marginBottom: '.2em', gap: '.5em' }}>- <TextInput ref={inputRef} onChange={onTextChange} value={text} autoComplete="off" autoCapitalize="off" autoCorrect="off" style={{ padding: '.3em' }} />+ <TextInput ref={inputRef} onChange={onTextChange} onBlur={onTextBlur} value={text} autoComplete="off" autoCapitalize="off" autoCorrect="off" style={{ padding: '.3em' }} />{generated != null && generated.fileNames.length > 1 && (<Dialog.Root>