diff --git a/packages/core/src/commands/clearContent.ts b/packages/core/src/commands/clearContent.ts new file mode 100644 index 0000000..f7d89da --- /dev/null +++ b/packages/core/src/commands/clearContent.ts @@ -0,0 +1,18 @@ +import type { NamedCommand } from "~/types/commands.ts"; +import { deleteContent } from "./deleteContent.ts"; + +declare module "@inkwell/core" { + interface Commands { + clearContent: { + /** + * Clears all content from the document. + * @returns A boolean indicating whether the command was executed successfully. + */ + clearContent: () => ReturnType; + }; + } +} + +export const clearContent: NamedCommand<"clearContent"> = (ctx) => () => { + return deleteContent(ctx)(0, ctx.state.doc.length); +}; diff --git a/packages/core/src/commands/cutContent.ts b/packages/core/src/commands/cutContent.ts index 9fe8a23..44cc9eb 100644 --- a/packages/core/src/commands/cutContent.ts +++ b/packages/core/src/commands/cutContent.ts @@ -20,18 +20,23 @@ declare module "@inkwell/core" { export const cutContent: NamedCommand<"cutContent"> = ({ state, dispatch }) => (fromRange, toRange) => { - const slicedContent = state.doc.sliceString(fromRange.from, fromRange.to); + try { + const slicedContent = state.doc.sliceString(fromRange.from, fromRange.to); - dispatch({ - changes: [ - { from: fromRange.from, to: fromRange.to, insert: "" }, - { - from: toRange.from + slicedContent.length, - to: toRange.to + slicedContent.length, - insert: slicedContent, - }, - ], - }); + dispatch({ + changes: [ + { from: fromRange.from, to: fromRange.to, insert: "" }, + { + from: toRange.from + slicedContent.length, + to: toRange.to + slicedContent.length, + insert: slicedContent, + }, + ], + }); - return true; + return true; + } catch (error) { + console.warn("[inkwell] Failed to cut content", fromRange, toRange, error); + return false; + } }; diff --git a/packages/core/src/commands/deleteContent.ts b/packages/core/src/commands/deleteContent.ts new file mode 100644 index 0000000..368b7cd --- /dev/null +++ b/packages/core/src/commands/deleteContent.ts @@ -0,0 +1,30 @@ +import type { NamedCommand } from "~/types/commands.ts"; + +declare module "@inkwell/core" { + interface Commands { + deleteContent: { + /** + * Deletes content from the document at the specified range. + * @param from The start position to delete the content. + * @param to The end position to delete the content. + * @returns A boolean indicating whether the command was executed successfully. + */ + deleteContent: (from: number, to: number) => boolean; + }; + } +} + +export const deleteContent: NamedCommand<"deleteContent"> = + ({ dispatch }) => + (from, to) => { + try { + dispatch({ + changes: { from, to, insert: "" }, + }); + + return true; + } catch (error) { + console.warn("[inkwell] Failed to delete content", from, to, error); + return false; + } + }; diff --git a/packages/core/src/commands/index.ts b/packages/core/src/commands/index.ts index 2f1eab6..f401485 100644 --- a/packages/core/src/commands/index.ts +++ b/packages/core/src/commands/index.ts @@ -1,3 +1,5 @@ +export * from "./clearContent.ts"; export * from "./cutContent.ts"; export * from "./insertContent.ts"; +export * from "./deleteContent.ts"; export * from "./setContent.ts"; diff --git a/packages/core/src/commands/insertContent.ts b/packages/core/src/commands/insertContent.ts index 965bde7..9bb5a6d 100644 --- a/packages/core/src/commands/insertContent.ts +++ b/packages/core/src/commands/insertContent.ts @@ -25,8 +25,13 @@ declare module "@inkwell/core" { export const insertContent: NamedCommand<"insertContent"> = ({ dispatch }) => ({ content, from, to }) => { - dispatch({ - changes: { from, to, insert: content }, - }); - return true; + try { + dispatch({ + changes: { from, to, insert: content }, + }); + return true; + } catch (error) { + console.warn("[inkwell] Failed to insert content", content, from, to, error); + return false; + } };