Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(md): delete btn and cmd-a override for markdown code boxes (#3607)

Seamus512928e8264a
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/accessory/CodeBoxAccessory.tsx b/js/app/packages/core/component/LexicalMarkdown/component/accessory/CodeBoxAccessory.tsx
index 548191f3bd..fba72e89b6 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/accessory/CodeBoxAccessory.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/accessory/CodeBoxAccessory.tsx
@@ -26,6 +26,7 @@ import FilePy from '@phosphor/file-py.svg';
import FileRs from '@phosphor/file-rs.svg';
import FileSql from '@phosphor/file-sql.svg';
import FileTs from '@phosphor/file-ts.svg';
+import TrashCan from '@phosphor/trash-simple.svg';
import { Button, cn, Dropdown } from '@ui';
import {
$getNodeByKey,
@@ -105,7 +106,7 @@ function CodeLanguageSelector(props: {
<Dropdown.Trigger
variant="ghost"
size="sm"
- class="text-ink-extra-muted/50 rounded-xs p-1.5"
+ class="text-ink-extra-muted/50 p-1.5"
tabIndex={-1}
>
<Dynamic
@@ -194,6 +195,14 @@ export function CodeBoxAccessory(props: {
}
};
+ const deleteCode = () => {
+ props.editor.update(() => {
+ const node = $getNodeByKey(props.nodeKey);
+ if (!$isCodeNode(node)) return;
+ node.remove();
+ });
+ };
+
const setLanguageOnNode = (language: string) => {
props.editor.update(() => {
const node = $getNodeByKey(props.nodeKey);
@@ -237,9 +246,9 @@ export function CodeBoxAccessory(props: {
setLanguage={setLanguageOnNode}
editor={props.editor}
/>
- <div class="flex gap-2 items-center h-full">
+ <div class="flex items-center h-full">
<Show when={showPreviewToggle()}>
- <div class="flex items-center gap-2">
+ <div class="flex items-center gap-2 mr-2">
<div class="text-xs text-ink-extra-muted/50">Preview</div>
<Switch
checked={isPreviewMode()}
@@ -255,10 +264,25 @@ export function CodeBoxAccessory(props: {
</Switch>
</div>
</Show>
+ <Show when={props.editor.isEditable()}>
+ <Button
+ variant="ghost"
+ size="icon-sm"
+ class="text-ink-extra-muted/50 hover:text-failure h-full"
+ tooltip="Delete Code"
+ on:click={(e) => {
+ e.stopPropagation();
+ e.preventDefault();
+ deleteCode();
+ }}
+ >
+ <TrashCan />
+ </Button>
+ </Show>
<Button
variant="ghost"
size="icon-sm"
- class="text-ink-extra-muted/50 rounded-xs h-full"
+ class="text-ink-extra-muted/50 h-full"
tooltip="Copy Code"
on:click={(e) => {
e.stopPropagation();
@@ -425,7 +449,7 @@ export const StaticCodeBoxAccessory = (props: {
<Button
variant="ghost"
size="icon-sm"
- class="text-ink-extra-muted/50 rounded-xs h-full"
+ class="text-ink-extra-muted/50 h-full"
tooltip="Copy Code"
on:click={(e) => {
e.stopPropagation();
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/code/codePlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/code/codePlugin.ts
index 6842246535..00aef2ff66 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/code/codePlugin.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/code/codePlugin.ts
@@ -4,7 +4,7 @@ import {
CodeNode,
registerCodeHighlighting,
} from '@lexical/code';
-import { mergeRegister } from '@lexical/utils';
+import { $findMatchingParent, mergeRegister } from '@lexical/utils';
import {
$createCustomCodeNode,
$isCustomCodeNode,
@@ -16,14 +16,17 @@ import {
$insertNodes,
$isLineBreakNode,
$isRangeSelection,
+ COMMAND_PRIORITY_HIGH,
COMMAND_PRIORITY_LOW,
COMMAND_PRIORITY_NORMAL,
createCommand,
KEY_DOWN_COMMAND,
type LexicalCommand,
type LexicalEditor,
+ type LexicalNode,
type NodeKey,
type RangeSelection,
+ SELECT_ALL_COMMAND,
} from 'lexical';
import type { SetStoreFunction } from 'solid-js/store';
import { CodeBoxAccessory } from '../../component/accessory/CodeBoxAccessory';
@@ -40,6 +43,11 @@ function $isCode(node: any): node is CodeNode | CustomCodeNode {
return $isCodeNode(node) || $isCustomCodeNode(node);
}
+function $getCodeAncestor(node: LexicalNode): CodeNode | CustomCodeNode | null {
+ const target = $findMatchingParent(node, $isCode);
+ return $isCode(target) ? target : null;
+}
+
function $getNodeKeysByLine(
node: CodeNode | CustomCodeNode
): Array<Set<NodeKey>> {
@@ -209,6 +217,35 @@ function registerCodePlugin(editor: LexicalEditor, props: CodePluginProps) {
COMMAND_PRIORITY_NORMAL
),
+ editor.registerCommand(
+ SELECT_ALL_COMMAND,
+ () => {
+ const selection = $getSelection();
+ if (!$isRangeSelection(selection)) return false;
+
+ const anchorCode = $getCodeAncestor(selection.anchor.getNode());
+ const focusCode = $getCodeAncestor(selection.focus.getNode());
+
+ if (!anchorCode || anchorCode.getKey() !== focusCode?.getKey())
+ return false;
+
+ const codeKey = anchorCode.getKey();
+ const codeSize = anchorCode.getChildrenSize();
+ const selectedText = selection.getTextContent();
+ const codeText = anchorCode.getTextContent();
+ const isCodeFullySelected =
+ selectedText === codeText &&
+ $getCodeAncestor(selection.anchor.getNode())?.getKey() === codeKey &&
+ $getCodeAncestor(selection.focus.getNode())?.getKey() === codeKey;
+
+ if (isCodeFullySelected) return false;
+
+ anchorCode.select(0, codeSize);
+ return true;
+ },
+ COMMAND_PRIORITY_HIGH
+ ),
+
editor.registerCommand(
KEY_DOWN_COMMAND,
({ key }) => {