@8btc/mditor 0.0.5 → 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 +1 -1
- package/src/index.ts +23 -1
- package/src/ts/util/attachLineNumbers.ts +15 -4
package/package.json
CHANGED
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 {
|
|
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
|
-
|
|
263
|
+
container.removeAttribute("data-linenumber");
|
|
252
264
|
return;
|
|
253
265
|
}
|
|
254
266
|
|
|
255
267
|
let lineNumber = -1;
|
|
256
268
|
|
|
257
|
-
|
|
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;
|