-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hyperlink creation feature (#332)
* Add useToolBar hook * Add hyperlink creation feature * Add text selection check before inserting hyperlink * Refactor `useToolBar` & extract `checkAndAddFormat` functon to a separate utility file * Change valid URL check regular expression to `validator` library * Fix missing validator package installation
- Loading branch information
Showing
6 changed files
with
156 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useCallback, useState } from "react"; | ||
import { FormatType, ToolBarState, useFormatUtils } from "./useFormatUtils"; | ||
import { ViewUpdate } from "@codemirror/view"; | ||
|
||
export const useToolBar = () => { | ||
const [toolBarState, setToolBarState] = useState<ToolBarState>({ | ||
show: false, | ||
position: { top: 0, left: 0 }, | ||
selectedFormats: new Set<FormatType>(), | ||
}); | ||
const { getFormatMarkerLength, checkAndAddFormat } = useFormatUtils(); | ||
|
||
const updateFormatBar = useCallback( | ||
(update: ViewUpdate) => { | ||
const { state, view } = update; | ||
const selection = state.selection.main; | ||
|
||
if (selection.empty) { | ||
setToolBarState((prev) => ({ | ||
...prev, | ||
show: false, | ||
selectedFormats: new Set(), | ||
})); | ||
return; | ||
} | ||
|
||
const coords = view.coordsAtPos(selection.from); | ||
if (!coords) return; | ||
|
||
const maxLength = getFormatMarkerLength(view.state, selection.from); | ||
const selectedTextStart = state.sliceDoc(selection.from - maxLength, selection.from); | ||
const selectedTextEnd = state.sliceDoc(selection.to, selection.to + maxLength); | ||
const formats = new Set<FormatType>(); | ||
const formatChecks = [ | ||
{ marker: "**", format: FormatType.BOLD }, | ||
{ marker: "_", format: FormatType.ITALIC }, | ||
{ marker: "`", format: FormatType.CODE }, | ||
{ marker: "~~", format: FormatType.STRIKETHROUGH }, | ||
]; | ||
|
||
formatChecks.forEach(({ marker, format }) => { | ||
checkAndAddFormat(selectedTextStart, selectedTextEnd, marker, format, formats); | ||
}); | ||
|
||
// TODO: Modify the rendering method so that it is not affected by the size of the Toolbar | ||
setToolBarState((prev) => ({ | ||
...prev, | ||
show: true, | ||
position: { | ||
top: coords.top - 5, | ||
left: coords.left, | ||
}, | ||
selectedFormats: formats, | ||
})); | ||
}, | ||
[getFormatMarkerLength, checkAndAddFormat] | ||
); | ||
|
||
return { toolBarState, setToolBarState, updateFormatBar }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { EditorView } from "codemirror"; | ||
import validator from "validator"; | ||
import { CodePairDocType } from "../store/editorSlice"; | ||
|
||
const isValidUrl = (url: string) => { | ||
return validator.isURL(url); | ||
}; | ||
|
||
const insertLinkToEditor = (url: string, view: EditorView, doc: CodePairDocType) => { | ||
const { from, to } = view.state.selection.main; | ||
const selectedText = view.state.sliceDoc(from, to); | ||
const insert = `[${selectedText}](${url})`; | ||
|
||
doc.update((root, presence) => { | ||
root.content.edit(from, to, insert); | ||
presence.set({ | ||
selection: root.content.indexRangeToPosRange([ | ||
from + insert.length, | ||
from + insert.length, | ||
]), | ||
}); | ||
}); | ||
|
||
view.dispatch({ | ||
changes: { from, to, insert }, | ||
selection: { | ||
anchor: from + insert.length, | ||
}, | ||
}); | ||
}; | ||
|
||
export const urlHyperlinkInserter = (doc: CodePairDocType) => { | ||
return EditorView.domEventHandlers({ | ||
paste(event, view) { | ||
const url = event.clipboardData?.getData("text/plain"); | ||
if (!url || !isValidUrl(url)) return; | ||
|
||
const { from, to } = view.state.selection.main; | ||
if (from === to) return; | ||
|
||
insertLinkToEditor(url, view, doc); | ||
event.preventDefault(); | ||
}, | ||
}); | ||
}; |