Files
simple-react-app-kit/packages/lexical/src/actions/text-format.ts
T

93 lines
2.0 KiB
TypeScript
Raw Normal View History

import {
Bold,
CaseLower,
CaseSensitive,
CaseUpper,
Italic,
Strikethrough,
Subscript,
Superscript,
Underline,
} from "@workspace/ui/components/icon"
import { $getSelection, $isRangeSelection, FORMAT_TEXT_COMMAND } from "lexical"
import type { TextFormatType } from "lexical"
import type { LexicalActionDefinition, LexicalActionName } from "../types"
import type { LexicalMessage } from "../i18n"
import { lexicalMessages } from "../messages"
function textFormatAction(
name: LexicalActionName,
label: LexicalMessage,
format: TextFormatType,
icon: LexicalActionDefinition["icon"]
): LexicalActionDefinition {
return {
name,
label,
icon,
execute: ({ editor }) => {
editor.dispatchCommand(FORMAT_TEXT_COMMAND, format)
},
isActive: ({ editor }) =>
editor.getEditorState().read(() => {
const selection = $getSelection()
return $isRangeSelection(selection) && selection.hasFormat(format)
}),
}
}
export const boldAction = textFormatAction(
"bold",
lexicalMessages.bold,
"bold",
Bold
)
export const italicAction = textFormatAction(
"italic",
lexicalMessages.italic,
"italic",
Italic
)
export const underlineAction = textFormatAction(
"underline",
lexicalMessages.underline,
"underline",
Underline
)
export const lowercaseAction = textFormatAction(
"lowercase",
lexicalMessages.lowercase,
"lowercase",
CaseLower
)
export const uppercaseAction = textFormatAction(
"uppercase",
lexicalMessages.uppercase,
"uppercase",
CaseUpper
)
export const capitalizeAction = textFormatAction(
"capitalize",
lexicalMessages.capitalize,
"capitalize",
CaseSensitive
)
export const strikethroughAction = textFormatAction(
"strikethrough",
lexicalMessages.strikethrough,
"strikethrough",
Strikethrough
)
export const subscriptAction = textFormatAction(
"subscript",
lexicalMessages.subscript,
"subscript",
Subscript
)
export const superscriptAction = textFormatAction(
"superscript",
lexicalMessages.superscript,
"superscript",
Superscript
)