mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-03 10:21:06 +05:30
74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import { create } from 'zustand';
|
|
import { immer } from 'zustand/middleware/immer';
|
|
import { enableMapSet } from 'immer';
|
|
|
|
enableMapSet();
|
|
|
|
export interface Buffer {
|
|
id: string;
|
|
path: string;
|
|
content: string;
|
|
dirty: boolean;
|
|
cursor?: { line: number; column: number };
|
|
}
|
|
|
|
interface EditorState {
|
|
buffers: Map<string, Buffer>;
|
|
activeId: string | null;
|
|
openBuffer: (id: string, path: string, content: string) => void;
|
|
updateContent: (id: string, content: string) => void;
|
|
markSaved: (id: string) => void;
|
|
setCursor: (id: string, line: number, column: number) => void;
|
|
closeBuffer: (id: string) => void;
|
|
setActive: (id: string) => void;
|
|
renameBuffer: (oldId: string, newId: string, newPath: string) => void;
|
|
}
|
|
|
|
export const useEditorStore = create<EditorState>()(
|
|
immer((set) => ({
|
|
buffers: new Map(),
|
|
activeId: null,
|
|
openBuffer: (id, path, content) =>
|
|
set((s) => {
|
|
s.buffers.set(id, { id, path, content, dirty: false });
|
|
s.activeId = id;
|
|
}),
|
|
updateContent: (id, content) =>
|
|
set((s) => {
|
|
const buf = s.buffers.get(id);
|
|
if (buf) {
|
|
buf.content = content;
|
|
buf.dirty = true;
|
|
}
|
|
}),
|
|
markSaved: (id) =>
|
|
set((s) => {
|
|
const buf = s.buffers.get(id);
|
|
if (buf) buf.dirty = false;
|
|
}),
|
|
setCursor: (id, line, column) =>
|
|
set((s) => {
|
|
const buf = s.buffers.get(id);
|
|
if (buf) buf.cursor = { line, column };
|
|
}),
|
|
closeBuffer: (id) =>
|
|
set((s) => {
|
|
s.buffers.delete(id);
|
|
if (s.activeId === id) s.activeId = null;
|
|
}),
|
|
setActive: (id) =>
|
|
set((s) => {
|
|
s.activeId = id;
|
|
}),
|
|
renameBuffer: (oldId, newId, newPath) =>
|
|
set((s) => {
|
|
const buf = s.buffers.get(oldId);
|
|
if (buf) {
|
|
s.buffers.delete(oldId);
|
|
s.buffers.set(newId, { ...buf, id: newId, path: newPath });
|
|
if (s.activeId === oldId) s.activeId = newId;
|
|
}
|
|
}),
|
|
}))
|
|
);
|