Skip to content

Commit 0878c91

Browse files
committed
Add useShowInvisibles extension
1 parent 981e41b commit 0878c91

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/extensions/search/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export type { SearchAPI, SearchFilter } from "./search"
33
export * from "./replace"
44
export * from "./widget"
55
export * from "./selection"
6+
export * from "./invisibles"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "prism-code-editor/invisibles.css";
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useLayoutEffect } from "react"
2+
import { useStableRef } from "../../core"
3+
import { PrismEditor } from "../../types"
4+
import { useEditorSearch } from "./search"
5+
6+
const useShowInvisibles = (editor: PrismEditor, alwaysShow?: boolean) => {
7+
const show = useStableRef([alwaysShow])
8+
const searchAPI = useEditorSearch(editor, "pce-invisibles")
9+
show[0] = alwaysShow
10+
11+
useLayoutEffect(() => {
12+
let prev: string
13+
const matches = searchAPI.matches
14+
const container = searchAPI.container
15+
const nodes = container.children
16+
const tabs: boolean[] = []
17+
const update = () => {
18+
const value = editor.value
19+
const [start, end] = editor.getSelection()
20+
21+
if (!show[0] || prev != (prev = value)) {
22+
searchAPI.search(" |\t", true, false, true, show[0] ? undefined : [start, end])
23+
for (let i = 0, l = matches.length; i < l; i++) {
24+
if ((value[matches[i][0]] == "\t") == !tabs[i]) {
25+
nodes[i].className = (tabs[i] = !tabs[i]) ? "pce-tab" : ""
26+
}
27+
}
28+
}
29+
}
30+
31+
if (editor.value) update()
32+
return editor.on("selectionChange", update)
33+
}, [])
34+
}
35+
36+
export { useShowInvisibles }

src/testsite/App.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import "../prism/languages/tsx"
1212
import "../prism/languages/jsdoc"
1313
import "./style.css"
1414
import "../extensions/search/search.css"
15+
import "../extensions/search/invisibles.css"
1516
import "../languages/jsx"
1617
import { useBracketMatcher } from "../extensions/match-brackets"
1718
import { useHightlightBracketPairs } from "../extensions/match-brackets/highlight"
1819
import { IndentGuides } from "../extensions/guides"
1920
import { PrismEditor } from "../types"
20-
import { useHighlightSelectionMatches } from "../extensions/search/selection"
21+
import { useHighlightSelectionMatches, useShowInvisibles } from "../extensions/search"
2122
import { useOverscroll } from "../extensions/overscroll"
2223
import { useHighlightMatchingTags, useTagMatcher } from "../extensions/match-tags"
2324
import { useDefaultCommands, useEditHistory } from "../extensions/commands"
@@ -65,9 +66,9 @@ const Extensions = ({ editor }: { editor: PrismEditor }) => {
6566
useHighlightSelectionMatches(editor)
6667
useCopyButton(editor)
6768
useCursorPosition(editor)
69+
useShowInvisibles(editor)
6870
useAutoComplete(editor, {
6971
filter: fuzzyFilter,
70-
closeOnBlur: false
7172
})
7273
return (
7374
<>

0 commit comments

Comments
 (0)