mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
Move to TanStack Start from Next.js (#184)
This commit is contained in:
12
web/shared/la-editor/lib/utils/index.ts
Normal file
12
web/shared/la-editor/lib/utils/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Editor } from "@tiptap/core"
|
||||
import { LAEditorProps } from "../../la-editor"
|
||||
|
||||
export function getOutput(editor: Editor, output: LAEditorProps["output"]) {
|
||||
if (output === "html") return editor.getHTML()
|
||||
if (output === "json") return editor.getJSON()
|
||||
if (output === "text") return editor.getText()
|
||||
return ""
|
||||
}
|
||||
|
||||
export * from "./isCustomNodeSelected"
|
||||
export * from "./isTextSelected"
|
||||
37
web/shared/la-editor/lib/utils/isCustomNodeSelected.ts
Normal file
37
web/shared/la-editor/lib/utils/isCustomNodeSelected.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { HorizontalRule } from "../../extensions/horizontal-rule"
|
||||
import { Link } from "../../extensions/link"
|
||||
import { Editor } from "@tiptap/react"
|
||||
|
||||
export const isTableGripSelected = (node: HTMLElement) => {
|
||||
let container = node
|
||||
|
||||
while (container && !["TD", "TH"].includes(container.tagName)) {
|
||||
container = container.parentElement!
|
||||
}
|
||||
|
||||
const gripColumn =
|
||||
container &&
|
||||
container.querySelector &&
|
||||
container.querySelector("a.grip-column.selected")
|
||||
const gripRow =
|
||||
container &&
|
||||
container.querySelector &&
|
||||
container.querySelector("a.grip-row.selected")
|
||||
|
||||
if (gripColumn || gripRow) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export const isCustomNodeSelected = (editor: Editor, node: HTMLElement) => {
|
||||
const customNodes = [HorizontalRule.name, Link.name]
|
||||
|
||||
return (
|
||||
customNodes.some((type) => editor.isActive(type)) ||
|
||||
isTableGripSelected(node)
|
||||
)
|
||||
}
|
||||
|
||||
export default isCustomNodeSelected
|
||||
26
web/shared/la-editor/lib/utils/isTextSelected.ts
Normal file
26
web/shared/la-editor/lib/utils/isTextSelected.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { isTextSelection } from "@tiptap/core"
|
||||
import { Editor } from "@tiptap/react"
|
||||
|
||||
export const isTextSelected = ({ editor }: { editor: Editor }) => {
|
||||
const {
|
||||
state: {
|
||||
doc,
|
||||
selection,
|
||||
selection: { empty, from, to },
|
||||
},
|
||||
} = editor
|
||||
|
||||
// Sometime check for `empty` is not enough.
|
||||
// Doubleclick an empty paragraph returns a node size of 2.
|
||||
// So we check also for an empty text size.
|
||||
const isEmptyTextBlock =
|
||||
!doc.textBetween(from, to).length && isTextSelection(selection)
|
||||
|
||||
if (empty || isEmptyTextBlock || !editor.isEditable) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
export default isTextSelected
|
||||
Reference in New Issue
Block a user