This is an automated email from the ASF dual-hosted git repository.
robinv8 pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git
The following commit(s) were added to refs/heads/dev by this push:
new 81005be1 fix(editor): make list toolbar actions work in empty editor
(#1629)
81005be1 is described below
commit 81005be1549ca0ed004047d93b0440bfe8c24b69
Author: Luke Gao <[email protected]>
AuthorDate: Mon Oct 12 10:48:53 2026 +0800
fix(editor): make list toolbar actions work in empty editor (#1629)
Fixes #1620
## Proposed Changes
- Read the active line directly from CodeMirror state instead of calling
the
adapter-only `getCursor()` method.
- Insert the appropriate Markdown marker (`1. ` or `- `) when a list
action is
used on a blank line.
- Add regression tests covering ordered and unordered list toolbar
actions
through the CodeMirror adapter.
- Move the comment border declaration before its nested rule to remove
the Sass
mixed-declaration warning.
## Verification
- `cd ui && pnpm test`
- `cd ui && pnpm build`
- ESLint and ASF header checks pass
---
ui/src/components/Comment/index.scss | 3 +-
.../Editor/utils/codemirror/commands.test.cjs | 82 ++++++++++++++++++++++
.../components/Editor/utils/codemirror/commands.ts | 30 ++++++--
3 files changed, 109 insertions(+), 6 deletions(-)
diff --git a/ui/src/components/Comment/index.scss
b/ui/src/components/Comment/index.scss
index 7264c66c..592863f9 100644
--- a/ui/src/components/Comment/index.scss
+++ b/ui/src/components/Comment/index.scss
@@ -23,6 +23,8 @@
.comments-wrap {
.comment-item {
+ border-bottom: 1px solid var(--an-comment-item-border-bottom);
+
&:hover {
@include media-breakpoint-up(md) {
.control-area {
@@ -30,7 +32,6 @@
}
}
}
- border-bottom: 1px solid var(--an-comment-item-border-bottom);
}
.fmt {
display: inline;
diff --git a/ui/src/components/Editor/utils/codemirror/commands.test.cjs
b/ui/src/components/Editor/utils/codemirror/commands.test.cjs
new file mode 100644
index 00000000..294f3851
--- /dev/null
+++ b/ui/src/components/Editor/utils/codemirror/commands.test.cjs
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const test = require('node:test');
+const ts = require('typescript');
+
+const previousTypeScriptLoader = require.extensions['.ts'];
+require.extensions['.ts'] = (module, filename) => {
+ const source = fs.readFileSync(filename, 'utf8');
+ const output = ts.transpileModule(source, {
+ compilerOptions: {
+ module: ts.ModuleKind.CommonJS,
+ target: ts.ScriptTarget.ES2020,
+ esModuleInterop: true,
+ },
+ fileName: filename,
+ }).outputText;
+ module._compile(output, filename);
+};
+
+const { EditorState } = require('@codemirror/state');
+let createCodeMirrorAdapter;
+try {
+ ({ createCodeMirrorAdapter } = require('./adapter.ts'));
+} finally {
+ if (previousTypeScriptLoader) {
+ require.extensions['.ts'] = previousTypeScriptLoader;
+ } else {
+ delete require.extensions['.ts'];
+ }
+}
+
+function createEditorView(doc = '') {
+ let state = EditorState.create({ doc });
+
+ return {
+ contentDOM: {
+ addEventListener() {},
+ focus() {},
+ removeEventListener() {},
+ },
+ get state() {
+ return state;
+ },
+ dispatch(spec) {
+ state = state.update(spec).state;
+ },
+ };
+}
+
+for (const [name, command, expected] of [
+ ['unordered', 'insertUnorderedList', '- '],
+ ['ordered', 'insertOrderedList', '1. '],
+]) {
+ test(`${name} list toolbar command inserts a marker in an empty editor`, ()
=> {
+ const view = createEditorView();
+ const editor = createCodeMirrorAdapter(view);
+
+ editor[command]();
+
+ assert.equal(view.state.doc.toString(), expected);
+ assert.equal(view.state.selection.main.head, expected.length);
+ });
+}
diff --git a/ui/src/components/Editor/utils/codemirror/commands.ts
b/ui/src/components/Editor/utils/codemirror/commands.ts
index 546382d4..153e530f 100644
--- a/ui/src/components/Editor/utils/codemirror/commands.ts
+++ b/ui/src/components/Editor/utils/codemirror/commands.ts
@@ -17,7 +17,7 @@
* under the License.
*/
-import { EditorSelection } from '@codemirror/state';
+import { EditorSelection, Line } from '@codemirror/state';
import { Editor, Level } from '../../types';
@@ -33,6 +33,22 @@ import { Editor, Level } from '../../types';
* @returns Object containing all command methods
*/
export function createCommandMethods(editor: Editor) {
+ const insertListMarkerOnBlankLine = (line: Line, marker: string) => {
+ if (line.text.trim() !== '') {
+ return false;
+ }
+
+ editor.dispatch({
+ changes: {
+ from: line.from,
+ to: line.to,
+ insert: marker,
+ },
+ selection: EditorSelection.cursor(line.from + marker.length),
+ });
+ return true;
+ };
+
// Create methods object that allows self-reference
const methods = {
wrapText: (before: string, after = before, defaultText) => {
@@ -128,8 +144,10 @@ export function createCommandMethods(editor: Editor) {
},
insertOrderedList: () => {
- const cursor = editor.getCursor();
- const line = editor.state.doc.line(cursor.line);
+ const line = editor.state.doc.lineAt(editor.state.selection.main.head);
+ if (insertListMarkerOnBlankLine(line, '1. ')) {
+ return;
+ }
const lineText = line.text.trim();
if (/^\d+\.\s/.test(lineText)) {
return;
@@ -143,8 +161,10 @@ export function createCommandMethods(editor: Editor) {
},
insertUnorderedList: () => {
- const cursor = editor.getCursor();
- const line = editor.state.doc.line(cursor.line);
+ const line = editor.state.doc.lineAt(editor.state.selection.main.head);
+ if (insertListMarkerOnBlankLine(line, '- ')) {
+ return;
+ }
const lineText = line.text.trim();
if (/^[-*+]\s/.test(lineText)) {
return;