@codemirror/view#lineNumbers TypeScript Examples
The following examples show how to use
@codemirror/view#lineNumbers.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: code.tsx From nota with MIT License | 5 votes |
Listing: React.FC<ListingProps> = props => {
let ctx = usePlugin(ListingPlugin);
let ref = useRef(null);
useEffect(() => {
let language = props.language || ctx.language;
let code = joinRecursive(props.children as any);
let parseResult = null;
if (props.delimiters) {
parseResult = parseWithDelimiters(code, props.delimiters.delimiters);
if (parseResult.error) {
throw parseResult.error;
} else {
code = parseResult.outputCode!;
}
}
let editor = new EditorView({
state: EditorState.create({
doc: code,
extensions: [
lineNumbers(),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
theme,
EditorView.editable.of(props.editable || false),
props.wrap || ctx.wrap ? EditorView.lineWrapping : [],
highlightField,
]
.concat(language ? [language] : [])
.concat(props.extensions || []),
}),
parent: ref.current!,
});
if (props.onLoad) {
props.onLoad(editor);
}
if (props.delimiters) {
props.delimiters.onParse(parseResult!.ranges!);
}
}, []);
return <div className="listing" ref={ref} />;
}
Example #2
Source File: index.tsx From pintora with MIT License | 4 votes |
Editor = (props: Props) => {
const { code, onCodeChange, editorOptions, errorInfo } = props
const wrapperRef = useRef<HTMLDivElement>()
const viewRef = useRef<EditorView>()
const stateRef = useRef<EditorState>()
useEffect(() => {
if (!wrapperRef.current) return
let editor = viewRef.current
let state = stateRef.current
if (viewRef.current) viewRef.current.destroy()
if (!state) {
const onUpdateExtension = EditorView.updateListener.of(update => {
if (update.docChanged && state) {
const newCode = update.view.state.doc.toJSON().join('\n')
onCodeChange(newCode)
}
})
const extensions: Extension[] = [
keymap.of(standardKeymap),
history(),
keymap.of(historyKeymap),
onUpdateExtension,
keymap.of(searchKeymap),
lineNumbers(),
search({ top: true }),
highlightActiveLine(),
oneDark,
keymap.of(tabKeymaps),
]
if (editorOptions.language === 'json') {
extensions.push(json())
}
state = EditorState.create({
doc: code,
extensions,
})
stateRef.current = state
}
editor = new EditorView({
parent: wrapperRef.current,
state: state,
})
viewRef.current = editor
return () => {
if (viewRef.current) {
viewRef.current.destroy()
}
}
}, [])
useEffect(() => {
const view = viewRef.current
if (view && view.state) {
const state = view.state
const currentCode = state.doc.toJSON().join('\n')
// console.log('currentCode == code', currentCode == code)
if (currentCode !== code) {
view.dispatch(state.update({ changes: { from: 0, to: currentCode.length, insert: code } }))
}
}
}, [code])
useEffect(() => {
const view = viewRef.current
if (view) {
if (errorInfo) {
const spec = setDiagnostics(view.state, [
{
severity: 'error',
message: errorInfo.message,
from: errorInfo.offset,
to: errorInfo.offset,
},
])
view.dispatch(spec)
} else {
view.dispatch(setDiagnostics(view.state, []))
}
}
}, [errorInfo])
// TOOD: highlight error
// useEffect(() => {
// }, [errorInfo])
return <div className="CMEditor" ref={wrapperRef as any}></div>
}