From d0d2f0ea196e4b1d7d28074cdfa62ebe7300a8cd Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sat, 2 Jul 2022 12:04:38 +0800 Subject: [PATCH] fix: update onStatistics result `line`. --- core/src/utils.ts | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/core/src/utils.ts b/core/src/utils.ts index 72fb9cd60..f791559d6 100644 --- a/core/src/utils.ts +++ b/core/src/utils.ts @@ -1,11 +1,13 @@ -import { EditorSelection, SelectionRange } from '@codemirror/state'; +import { EditorSelection, SelectionRange, Line } from '@codemirror/state'; import { ViewUpdate } from '@codemirror/view'; export interface Statistics { - /** Get the number of lines in the editor. */ - lineCount: number; /** total length of the document */ length: number; + /** Get the number of lines in the editor. */ + lineCount: number; + /** Get the currently line description around the given position. */ + line: Line; /** Get the proper [line-break](https://codemirror.net/docs/ref/#state.EditorState^lineSeparator) string for this state. */ lineBreak: string; /** Returns true when the editor is [configured](https://codemirror.net/6/docs/ref/#state.EditorState^readOnly) to be read-only. */ @@ -29,16 +31,19 @@ export interface Statistics { selectedText: boolean; } -export const getStatistics = (view: ViewUpdate): Statistics => ({ - lineCount: view.state.doc.lines, - length: view.state.doc.length, - lineBreak: view.state.lineBreak, - readOnly: view.state.readOnly, - tabSize: view.state.tabSize, - selection: view.state.selection, - selectionAsSingle: view.state.selection.asSingle().main, - ranges: view.state.selection.ranges, - selectionCode: view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to), - selections: view.state.selection.ranges.map((r) => view.state.sliceDoc(r.from, r.to)), - selectedText: view.state.selection.ranges.some((r) => !r.empty), -}); +export const getStatistics = (view: ViewUpdate): Statistics => { + return { + line: view.state.doc.lineAt(view.state.selection.main.from), + lineCount: view.state.doc.lines, + lineBreak: view.state.lineBreak, + length: view.state.doc.length, + readOnly: view.state.readOnly, + tabSize: view.state.tabSize, + selection: view.state.selection, + selectionAsSingle: view.state.selection.asSingle().main, + ranges: view.state.selection.ranges, + selectionCode: view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to), + selections: view.state.selection.ranges.map((r) => view.state.sliceDoc(r.from, r.to)), + selectedText: view.state.selection.ranges.some((r) => !r.empty), + }; +};