@8btc/mditor 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@8btc/mditor",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "mditor, Md编辑器",
5
5
  "author": "wujie-vditor",
6
6
  "jsdelivr": "dist/index.min.js",
package/src/index.ts CHANGED
@@ -47,7 +47,10 @@ import {
47
47
  insertEmptyBlock,
48
48
  } from "./ts/util/fixBrowserBehavior";
49
49
  import { accessLocalStorage } from "./ts/util/compatibility";
50
- import { attachLineNumbersToBlocks } from "./ts/util/attachLineNumbers";
50
+ import {
51
+ attachLineNumbersToBlocks,
52
+ attachLineNumbersToBlocksThrottled,
53
+ } from "./ts/util/attachLineNumbers";
51
54
 
52
55
  /**
53
56
  * 设置所有 details 默认展开,并确保不影响内容展示与可访问性
@@ -191,6 +194,25 @@ class Vditor extends VditorMethod {
191
194
  return getMarkdown(this.vditor);
192
195
  }
193
196
 
197
+ /**
198
+ * 手动更新行号
199
+ * - 读取当前模式下的 Markdown 文本并为对应编辑区域同步/节流写入 data-linenumber
200
+ * - 依赖配置 `lineNumber: true`,关闭时直接跳过以提升性能
201
+ * @param immediate 为 true 时立即更新;默认为节流更新以避免频繁调用
202
+ */
203
+ public updateLineNumbers(immediate = false) {
204
+ if (!this.vditor.options.lineNumber) {
205
+ return;
206
+ }
207
+ const text = getMarkdown(this.vditor);
208
+ const root = this.vditor[this.vditor.currentMode].element;
209
+ if (immediate) {
210
+ attachLineNumbersToBlocks(root, text);
211
+ } else {
212
+ attachLineNumbersToBlocksThrottled(root, text);
213
+ }
214
+ }
215
+
194
216
  /** 获取编辑器当前编辑模式 */
195
217
  public getCurrentMode() {
196
218
  return this.vditor.currentMode;
@@ -241,21 +241,32 @@ export const attachLineNumbersToBlocks = (
241
241
  const container = el as HTMLElement;
242
242
  const dataType = container.getAttribute("data-type") || "";
243
243
  if (dataType === "math-block" || dataType === "code-block") {
244
- // container.setAttribute("data-linenumber", "");
245
244
  return;
246
245
  }
246
+
247
+ // 仅 li 需要行号:跳过并清理 ul/ol 自身及其内部嵌套的 p/div 等块
248
+ const tagName = container.tagName;
249
+ if (tagName === "UL" || tagName === "OL") {
250
+ container.removeAttribute("data-linenumber");
251
+ return;
252
+ }
253
+ const liAncestor = container.closest("li");
254
+ if (liAncestor) {
255
+ container.removeAttribute("data-linenumber");
256
+ return;
257
+ }
258
+
247
259
  const text = container.textContent || "";
248
260
  const normText = normalize(text);
249
261
  // 跳过纯空白块
250
262
  if (!normText.trim()) {
251
- // container.setAttribute("data-linenumber", "");
263
+ container.removeAttribute("data-linenumber");
252
264
  return;
253
265
  }
254
266
 
255
267
  let lineNumber = -1;
256
268
 
257
- const tag = container.tagName;
258
- if (tag === "BLOCKQUOTE") {
269
+ if (tagName === "BLOCKQUOTE") {
259
270
  const firstLine =
260
271
  normText.split("\n").find((l) => l.trim().length > 0) ||
261
272
  normText;