Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/components/CommandMenu/SlashCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface SlashCommand {
content?: string;
action?: (view: EditorView, from: number, to: number) => void;
inline?: boolean;
modalId?: string;
}

export const slashCommands: SlashCommand[] = [
Expand Down Expand Up @@ -233,4 +234,18 @@ export const slashCommands: SlashCommand[] = [
);
},
},
{
label: "license",
description: "Add a license or copyright statement",
modalId: "license",
action: (view, from, to) => {
// Remove the /license command text
view.dispatch(
view.state.update({
changes: { from, to, insert: "" },
selection: { anchor: from },
}),
);
},
},
];
34 changes: 33 additions & 1 deletion src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import "rehype-callouts/theme/github";
interface EditorProps {
value: string;
onChange: (value: string) => void;
onOpenLicenseModal?: (insertCallback: (text: string) => void) => void;
}

export default function Editor({ value, onChange }: EditorProps) {
export default function Editor({ value, onChange, onOpenLicenseModal }: EditorProps) {
const editorRef = useRef<ReactCodeMirrorRef>(null);
const [showMenu, setShowMenu] = useState(false);
const [searchQuery, setSearchQuery] = useState("");
Expand Down Expand Up @@ -68,12 +69,43 @@ export default function Editor({ value, onChange }: EditorProps) {
return () => clearTimeout(timer);
}, []);

const insertText = (text: string) => {
const view = editorRef.current?.view;
if (!view) return;
const cursorPos = view.state.selection.main.head;

view.dispatch(
view.state.update({
changes: { from: cursorPos, to: cursorPos, insert: text },
selection: { anchor: cursorPos + text.length },
}),
);
view.focus();
};

const doInsert = (command: SlashCommand) => {
const view = editorRef.current?.view;
if (!view) return;
const cursorPos = view.state.selection.main.head;
const from = menuStartPosRef.current;

// Handle modal-triggering commands
if (command.modalId === "license" && onOpenLicenseModal) {
// Remove the /license command text
view.dispatch(
view.state.update({
changes: { from, to: cursorPos, insert: "" },
selection: { anchor: from },
}),
);
setShowMenu(false);
view.focus();

// Open the license modal with a callback to insert selected license
onOpenLicenseModal(insertText);
return;
}

if (command.action) {
command.action(view, from, cursorPos);
} else if (command.content) {
Expand Down
Loading