Skip to content

Commit

Permalink
fix(dcd): pull apart ToolbarPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-stumpf committed Jul 29, 2024
1 parent 03f1d7c commit b246b81
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
24 changes: 2 additions & 22 deletions src/components/lexical/plugins/ToolbarPlugin/ToolbarPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
$createQuoteNode,
$isHeadingNode,
} from "@lexical/rich-text"
import { $isAtNodeEnd, $wrapNodes } from "@lexical/selection"
import { $wrapNodes } from "@lexical/selection"
import { $getNearestNodeOfType, mergeRegister } from "@lexical/utils"
import {
ArrowClockwise,
Expand Down Expand Up @@ -56,8 +56,7 @@ import {
import { useCallback, useEffect, useRef, useState } from "react"
import ImageEditor from "./components/ImageEditor"
import LinkEditor from "./components/LinkEditor"

export const LOW_PRIORITY = 1
import { LOW_PRIORITY } from "./constants"

const supportedBlockTypes = [
"paragraph",
Expand Down Expand Up @@ -94,25 +93,6 @@ const blockTypeToBlockIcon: Record<
code: Code,
}

export const getSelectedNode = (selection) => {
const anchor = selection.anchor
const focus = selection.focus
const anchorNode = selection.anchor.getNode()
const focusNode = selection.focus.getNode()

if (anchorNode === focusNode) {
return anchorNode
}

const isBackward = selection.isBackward()

if (isBackward) {
return $isAtNodeEnd(focus) ? anchorNode : focusNode
} else {
return $isAtNodeEnd(anchor) ? focusNode : anchorNode
}
}

const BlockOptionsMenuList = ({
editor,
blockType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { $getSelection, $isRangeSelection, SELECTION_CHANGE_COMMAND } from "lexi
import { useCallback, useEffect, useRef, useState } from "react"
import { FormProvider, useForm } from "react-hook-form"
import { ensureUrlProtocol } from "utils/ensureUrlProtocol"
import { LOW_PRIORITY, getSelectedNode } from "../ToolbarPlugin"
import { LOW_PRIORITY } from "../ToolbarPlugin/constants"
import { getSelectedNode } from "../ToolbarPlugin/utils"

type LinkEditorProps = {
isOpen: boolean
Expand Down
1 change: 1 addition & 0 deletions src/components/lexical/plugins/ToolbarPlugin/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LOW_PRIORITY = 1
18 changes: 18 additions & 0 deletions src/components/lexical/plugins/ToolbarPlugin/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const getSelectedNode = (selection) => {
const anchor = selection.anchor
const focus = selection.focus
const anchorNode = selection.anchor.getNode()
const focusNode = selection.focus.getNode()

if (anchorNode === focusNode) {
return anchorNode
}

const isBackward = selection.isBackward()

if (isBackward) {
return $isAtNodeEnd(focus) ? anchorNode : focusNode
} else {
return $isAtNodeEnd(anchor) ? focusNode : anchorNode
}
}

0 comments on commit b246b81

Please sign in to comment.