-
Notifications
You must be signed in to change notification settings - Fork 133
feat: add configurable paste mode #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import type { | |
| EditorWidth, | ||
| CustomColors, | ||
| ThemeColorKey, | ||
| PasteMode, | ||
| } from "../types/note"; | ||
|
|
||
| type ThemeMode = "light" | "dark" | "system"; | ||
|
|
@@ -117,6 +118,8 @@ interface ThemeContextType { | |
| setCustomColor: (mode: "light" | "dark", key: ThemeColorKey, value: string) => void; | ||
| resetCustomColor: (mode: "light" | "dark", key: ThemeColorKey) => void; | ||
| resetAllCustomColors: (mode: "light" | "dark") => void; | ||
| pasteMode: PasteMode; | ||
| setPasteMode: (mode: PasteMode) => void; | ||
| } | ||
|
|
||
| const ThemeContext = createContext<ThemeContextType | null>(null); | ||
|
|
@@ -189,6 +192,7 @@ export function ThemeProvider({ children }: ThemeProviderProps) { | |
| ); | ||
| const [customColorsLight, setCustomColorsLightState] = useState<CustomColors>({}); | ||
| const [customColorsDark, setCustomColorsDarkState] = useState<CustomColors>({}); | ||
| const [pasteMode, setPasteModeState] = useState<PasteMode>("markdown"); | ||
| const [isInitialized, setIsInitialized] = useState(false); | ||
|
|
||
| const [systemTheme, setSystemTheme] = useState<"light" | "dark">(() => { | ||
|
|
@@ -248,6 +252,13 @@ export function ThemeProvider({ children }: ThemeProviderProps) { | |
| if (settings.customColorsDark) { | ||
| setCustomColorsDarkState(settings.customColorsDark); | ||
| } | ||
| if ( | ||
| settings.pasteMode === "markdown" || | ||
| settings.pasteMode === "plain" || | ||
| settings.pasteMode === "code-block" | ||
| ) { | ||
| setPasteModeState(settings.pasteMode); | ||
| } | ||
| } catch { | ||
| // If settings can't be loaded, use defaults | ||
| } | ||
|
|
@@ -544,6 +555,16 @@ export function ThemeProvider({ children }: ThemeProviderProps) { | |
| [], | ||
| ); | ||
|
|
||
| const setPasteMode = useCallback(async (mode: PasteMode) => { | ||
| setPasteModeState(mode); | ||
| try { | ||
| const settings = await getSettings(); | ||
| await updateSettings({ ...settings, pasteMode: mode }); | ||
| } catch (error) { | ||
|
Comment on lines
+558
to
+563
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid read-modify-write overwrites when persisting This setter reads the full settings object and writes it back with one field changed. Concurrent setting updates can clobber each other and drop unrelated fields. Prefer an atomic backend patch command (e.g., update only 🤖 Prompt for AI Agents |
||
| console.error("Failed to save paste mode:", error); | ||
| } | ||
| }, []); | ||
|
|
||
| // Live CSS variable update during drag (no persistence) | ||
| const setEditorMaxWidthLive = useCallback((value: string) => { | ||
| document.documentElement.style.setProperty("--editor-max-width", value); | ||
|
|
@@ -579,6 +600,8 @@ export function ThemeProvider({ children }: ThemeProviderProps) { | |
| setCustomColor, | ||
| resetCustomColor, | ||
| resetAllCustomColors, | ||
| pasteMode, | ||
| setPasteMode, | ||
| }} | ||
| > | ||
| {children} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guarantee code-block mode even when markdown parse fails.
If parse fails (or manager is unavailable), this returns
false, so default paste behavior runs instead of code-block mode. Add a deterministic fallback that inserts acodeBlocknode and still returnstrue.Suggested fix
if (pasteMode === "code-block") { + const normalized = text.replace(/\r\n/g, "\n"); const manager = currentEditor.storage.markdown?.manager; if (manager && typeof manager.parse === "function") { try { - const fenced = "```\n" + text + "\n```"; + const fenced = "```\n" + normalized + "\n```"; const parsed = manager.parse(fenced); if (parsed) { currentEditor.commands.insertContent(parsed); return true; } } catch { // fall through to default } } - return false; + currentEditor + .chain() + .focus() + .insertContent({ + type: "codeBlock", + attrs: { language: null }, + content: normalized ? [{ type: "text", text: normalized }] : [], + }) + .run(); + return true; }📝 Committable suggestion
🤖 Prompt for AI Agents