Skip to content

Commit 1e5c3a8

Browse files
committed
generalize toolbar to use more commands
1 parent 43957f4 commit 1e5c3a8

File tree

9 files changed

+246
-141
lines changed

9 files changed

+246
-141
lines changed

docs/commands.rst

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ Toggle the format of the current selected text in one of 'bold', 'underline', '
3131
Example: ``formatText bold``
3232

3333

34+
Clear Formatting
35+
^^^^^^^^^^^^^^^^
36+
37+
Clear all formatting of the selected text.
38+
39+
Example: ``clearFormatting``
40+
41+
3442
Undo
3543
^^^^
3644

@@ -87,6 +95,32 @@ Insert a horizontal line.
8795
Example: ``insertHorizontalLine``
8896

8997

98+
Insert Quote
99+
^^^^^^^^^^^^
100+
101+
Insert a quote node.
102+
103+
Example: ``insertQuote``
104+
105+
106+
Insert Code Block
107+
^^^^^^^^^^^^^^^^^
108+
109+
Insert a code block node.
110+
111+
Example: ``insertCodeBlock``
112+
113+
114+
Insert Heading
115+
^^^^^^^^^^^^^^
116+
117+
Insert a heading node of the specified level.
118+
119+
Example: ``insertHeading 1``
120+
121+
Example: ``insertHeading 6``
122+
123+
90124
Insert Typst Code
91125
^^^^^^^^^^^^^^^^^
92126

@@ -98,10 +132,14 @@ Example: ``insertTypstCode``
98132
Insert Image
99133
^^^^^^^^^^^^
100134

101-
Insert an image node with the given source URL.
135+
Insert an image node with the given source URL. If no argument is given, open a dialog to pick an image.
136+
137+
Note: images are not supported on the web version due to local storage limit concerns.
102138

103139
Example: ``insertImage ./example.png``
104140

141+
Example: ``insertImage``
142+
105143

106144
Insert Function Call
107145
^^^^^^^^^^^^^^^^^^^^
@@ -193,12 +231,12 @@ Removes the current column in the current table.
193231
Example: ``tableRemoveColumn``
194232

195233

196-
Toggle Link
197-
^^^^^^^^^^^
234+
Set Link
235+
^^^^^^^^
198236

199-
Toggles the link of the currently selected text to equal the given URL.
237+
Sets the link of the currently selected text to equal the given URL.
200238

201-
Example: ``toggleLink https://tyx-editor.com``
239+
Example: ``setLink https://tyx-editor.com``
202240

203241

204242
Open Link Popup
@@ -249,6 +287,14 @@ Opens the file save as dialog.
249287
Example: ``fileSaveAs``
250288

251289

290+
File Export
291+
~~~~~~~~~~~
292+
293+
Exports the file to the given format. Currently only ``typst`` is supported.
294+
295+
Example: ``fileExport typst``
296+
297+
252298
File Close
253299
~~~~~~~~~~
254300

src/commands.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ import {
3939
} from "./components/plugins/tableCommands"
4040
import { INSERT_TYPST_CODE_COMMAND } from "./components/plugins/typstCode"
4141
import {
42+
CLEAR_FORMATTING_COMMAND,
4243
FILE_CLOSE_COMMAND,
44+
FILE_EXPORT_COMMAND,
4345
FILE_NEW_COMMAND,
4446
FILE_NEW_FROM_TEMPLATE_COMMAND,
4547
FILE_OPEN_COMMAND,
4648
FILE_PREVIEW_COMMAND,
4749
FILE_SAVE_AS_COMMAND,
4850
FILE_SAVE_COMMAND,
51+
INSERT_CODE_BLOCK_COMMAND,
52+
INSERT_HEADING_COMMAND,
53+
INSERT_QUOTE_COMMAND,
4954
OPEN_DOCUMENT_SETTINGS_COMMAND,
5055
OPEN_LINK_POPUP_COMMAND,
5156
OPEN_SETTINGS_COMMAND,
@@ -55,6 +60,7 @@ const COMMANDS: Record<string, LexicalCommand<any>> = {
5560
toggleKeyboardMap: TOGGLE_KEYBOARD_MAP_COMMAND,
5661
formatElement: FORMAT_ELEMENT_COMMAND,
5762
formatText: FORMAT_TEXT_COMMAND,
63+
clearFormatting: CLEAR_FORMATTING_COMMAND,
5864
undo: UNDO_COMMAND,
5965
redo: REDO_COMMAND,
6066
insertMath: INSERT_MATH_COMMAND,
@@ -65,6 +71,9 @@ const COMMANDS: Record<string, LexicalCommand<any>> = {
6571
insertTypstCode: INSERT_TYPST_CODE_COMMAND,
6672
insertImage: INSERT_IMAGE_COMMAND,
6773
insertFunctionCall: INSERT_FUNCTION_CALL_COMMAND,
74+
insertQuote: INSERT_QUOTE_COMMAND,
75+
insertCodeBlock: INSERT_CODE_BLOCK_COMMAND,
76+
insertHeading: INSERT_HEADING_COMMAND,
6877
toggleMathInline: TOGGLE_MATH_INLINE_COMMAND,
6978
math: MATH_COMMAND,
7079
indent: INDENT_CONTENT_COMMAND,
@@ -74,13 +83,14 @@ const COMMANDS: Record<string, LexicalCommand<any>> = {
7483
tableInsertColumnRight: TABLE_INSERT_COLUMN_RIGHT_COMMAND,
7584
tableRemoveRow: TABLE_REMOVE_ROW_COMMAND,
7685
tableRemoveColumn: TABLE_REMOVE_COLUMN_COMMAND,
77-
toggleLink: TOGGLE_LINK_COMMAND,
86+
setLink: TOGGLE_LINK_COMMAND,
7887
openLinkPopup: OPEN_LINK_POPUP_COMMAND,
7988
fileOpen: FILE_OPEN_COMMAND,
8089
fileNew: FILE_NEW_COMMAND,
8190
fileNewFromTemplate: FILE_NEW_FROM_TEMPLATE_COMMAND,
8291
fileSave: FILE_SAVE_COMMAND,
8392
fileSaveAs: FILE_SAVE_AS_COMMAND,
93+
fileExport: FILE_EXPORT_COMMAND,
8494
fileClose: FILE_CLOSE_COMMAND,
8595
filePreview: FILE_PREVIEW_COMMAND,
8696
openSettings: OPEN_SETTINGS_COMMAND,
Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
import { ActionIcon, ActionIconProps, Tooltip } from "@mantine/core"
2+
import { forwardRef } from "react"
23
import { executeCommandSequence } from "../commands"
34
import { setLocalStorage } from "../utilities/hooks"
45

5-
const CommandActionIcon = (
6-
props: ActionIconProps & { component?: any; command: string; label?: string },
7-
) => {
8-
const icon = (
9-
<ActionIcon
10-
onMouseEnter={() => setLocalStorage("Hover Command", props.command)}
11-
onMouseLeave={() => setLocalStorage("Hover Command", null)}
12-
onClick={(e: any) => {
13-
e.preventDefault()
14-
e.stopPropagation()
15-
executeCommandSequence(props.command)
16-
}}
17-
{...props}
18-
/>
19-
)
6+
const CommandActionIcon = forwardRef(
7+
(
8+
props: ActionIconProps & {
9+
component?: any
10+
command: string
11+
label?: string
12+
},
13+
ref,
14+
) => {
15+
const icon = (
16+
<ActionIcon
17+
ref={ref}
18+
onMouseEnter={() => setLocalStorage("Hover Command", props.command)}
19+
onMouseLeave={() => setLocalStorage("Hover Command", null)}
20+
onClick={(e: any) => {
21+
e.preventDefault()
22+
e.stopPropagation()
23+
executeCommandSequence(props.command)
24+
}}
25+
{...props}
26+
/>
27+
)
2028

21-
if (props.label) {
22-
return <Tooltip label={props.label}>{icon}</Tooltip>
23-
}
29+
if (props.label) {
30+
return <Tooltip label={props.label}>{icon}</Tooltip>
31+
}
2432

25-
return icon
26-
}
33+
return icon
34+
},
35+
)
2736

2837
export default CommandActionIcon

src/components/plugins/ImagePlugin.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"
22
import { $insertNodes, COMMAND_PRIORITY_EDITOR } from "lexical"
33
import { useEffect, useState } from "react"
4-
import { readImage } from "../../backend"
4+
import { insertImage, readImage } from "../../backend"
55
import { getCurrentDocument } from "../../utilities"
66
import { $createImageNode, INSERT_IMAGE_COMMAND } from "./image"
77

@@ -24,6 +24,11 @@ const ImagePlugin = () => {
2424
return editor.registerCommand(
2525
INSERT_IMAGE_COMMAND,
2626
(src) => {
27+
if (src === undefined) {
28+
insertImage()
29+
return true
30+
}
31+
2732
$insertNodes([$createImageNode(src)])
2833
return true
2934
},

0 commit comments

Comments
 (0)