md-editor-v3 4.16.3 → 4.16.5

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.
@@ -1234,6 +1234,9 @@ const ToolBar = /* @__PURE__ */ vue.defineComponent({
1234
1234
  }
1235
1235
  });
1236
1236
  const DATA_LINE_SELECTOR = `.${config.prefix}-preview > [data-line]`;
1237
+ const getComputedStyleNum = (ele, key) => {
1238
+ return +getComputedStyle(ele).getPropertyValue(key).replace("px", "");
1239
+ };
1237
1240
  const scrollAutoWithScale = (pEle, cEle) => {
1238
1241
  const addEvent = util.debounce(() => {
1239
1242
  pEle.removeEventListener("scroll", scrollHandler);
@@ -1279,30 +1282,28 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1279
1282
  const { view: view2 } = codeMirrorUt;
1280
1283
  const smoothScroll = util.createSmoothScroll();
1281
1284
  const getTopByLine = (line) => {
1282
- return view2.lineBlockAt(view2.state.doc.line(line).from).top;
1285
+ return view2.lineBlockAt(view2.state.doc.line(line + 1).from).top;
1283
1286
  };
1284
- const getHeightByLine = (line) => {
1285
- return view2.lineBlockAt(view2.state.doc.line(line).from).height;
1287
+ const getBottomByLine = (line) => {
1288
+ return view2.lineBlockAt(view2.state.doc.line(line + 1).from).bottom;
1286
1289
  };
1287
1290
  let blockMap = [];
1288
1291
  let elesHasLineNumber = [];
1289
1292
  let startLines = [];
1290
1293
  const buildMap = () => {
1291
1294
  blockMap = [];
1292
- elesHasLineNumber = [];
1293
- startLines = [];
1294
- elesHasLineNumber = elesHasLineNumber.concat(
1295
- Array.from(cEle.querySelectorAll(DATA_LINE_SELECTOR))
1295
+ elesHasLineNumber = Array.from(
1296
+ cEle.querySelectorAll(DATA_LINE_SELECTOR)
1296
1297
  );
1297
- const startLinesCalc = elesHasLineNumber.map((item) => Number(item.dataset.line) + 1);
1298
- startLines = startLines.concat(startLinesCalc);
1298
+ startLines = elesHasLineNumber.map((item) => Number(item.dataset.line));
1299
+ const tempStartLines = [...startLines];
1299
1300
  const { lines } = view2.state.doc;
1300
- let start = 1;
1301
- let end = startLinesCalc.shift() ?? lines;
1302
- for (let i = 1; i <= lines; i++) {
1301
+ let start = tempStartLines.shift() || 0;
1302
+ let end = tempStartLines.shift() || lines;
1303
+ for (let i = 0; i < lines; i++) {
1303
1304
  if (i === end) {
1304
1305
  start = i;
1305
- end = startLinesCalc.shift() || lines + 1;
1306
+ end = tempStartLines.shift() || lines;
1306
1307
  }
1307
1308
  blockMap.push({
1308
1309
  start,
@@ -1313,44 +1314,61 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1313
1314
  let pLock = 0;
1314
1315
  let cLock = 0;
1315
1316
  const pEleHandler = () => {
1316
- var _a, _b;
1317
+ var _a, _b, _c;
1317
1318
  if (cLock !== 0) {
1318
1319
  return false;
1319
1320
  }
1320
1321
  pLock++;
1321
1322
  const { scrollDOM, contentHeight } = view2;
1322
- let cElePaddingTop = +getComputedStyle(cEle).paddingTop.replace("px", "");
1323
+ let cElePaddingTop = getComputedStyleNum(cEle, "padding-top");
1323
1324
  const blockInfo = view2.lineBlockAtHeight(scrollDOM.scrollTop);
1324
1325
  const { number: currLine } = view2.state.doc.lineAt(blockInfo.from);
1325
1326
  const blockData = blockMap[currLine - 1];
1326
1327
  if (!blockData) {
1327
1328
  return false;
1328
1329
  }
1329
- let endLinePosition = getTopByLine(blockData.end) + getHeightByLine(blockData.end);
1330
- let scale = 0;
1331
- const startTop = getTopByLine(blockData.start);
1332
- const startEle = cEle.querySelector(`[data-line="${blockData.start - 1}"]`) || ((_a = cEle.firstElementChild) == null ? void 0 : _a.firstElementChild);
1333
- const endEle = cEle.querySelector(`[data-line="${blockData.end}"]`) || ((_b = cEle.lastElementChild) == null ? void 0 : _b.lastElementChild);
1334
- let blockHeight = 0;
1335
- let startEleOffetTop = 0;
1330
+ let scale = 1;
1331
+ const startEle = cEle.querySelector(`[data-line="${blockData.start}"]`) || ((_a = cEle.firstElementChild) == null ? void 0 : _a.firstElementChild);
1332
+ const endEle = cEle.querySelector(`[data-line="${blockData.end + 1}"]`) || ((_b = cEle.lastElementChild) == null ? void 0 : _b.lastElementChild);
1333
+ const pMaxScrollLength = scrollDOM.scrollHeight - scrollDOM.clientHeight;
1334
+ const cMaxScrollLength = cEle.scrollHeight - cEle.clientHeight;
1335
+ let startTop = getTopByLine(blockData.start);
1336
+ let endBottom = getBottomByLine(blockData.end);
1337
+ let startEleOffetTop = startEle.offsetTop;
1338
+ let blockHeight = endEle.offsetTop - startEleOffetTop;
1336
1339
  if (startTop === 0) {
1337
- scale = scrollDOM.scrollTop / endLinePosition;
1340
+ startEleOffetTop = 0;
1338
1341
  if (startEle === endEle) {
1339
1342
  cElePaddingTop = 0;
1340
- endLinePosition = contentHeight - scrollDOM.offsetHeight;
1341
- blockHeight = endEle.offsetTop + endEle.offsetHeight - cEle.clientHeight;
1343
+ endBottom = contentHeight - scrollDOM.offsetHeight;
1344
+ blockHeight = cMaxScrollLength;
1342
1345
  } else {
1343
1346
  blockHeight = endEle.offsetTop;
1344
1347
  }
1345
- } else {
1346
- scale = (scrollDOM.scrollTop - startTop) / (endLinePosition - startTop);
1347
- startEleOffetTop = startEle.offsetTop;
1348
- blockHeight = endEle.offsetTop - startEleOffetTop;
1349
1348
  }
1350
- if (endLinePosition >= scrollDOM.scrollHeight - scrollDOM.clientHeight) {
1351
- scale = (scrollDOM.scrollTop - startTop) / (scrollDOM.scrollHeight - scrollDOM.clientHeight - startTop);
1352
- startEleOffetTop = startEle.offsetTop;
1353
- blockHeight = cEle.scrollHeight - cEle.clientHeight - startEleOffetTop + 10;
1349
+ scale = (scrollDOM.scrollTop - startTop + getComputedStyleNum(view2.contentDOM, "padding-bottom")) / (endBottom - startTop);
1350
+ if (startTop > 0 && (endBottom >= pMaxScrollLength || endEle.offsetTop + endEle.clientHeight > cMaxScrollLength)) {
1351
+ let lineNumer = 0;
1352
+ for (let i = elesHasLineNumber.length - 1; i >= 0; i--) {
1353
+ const curr = elesHasLineNumber[i];
1354
+ const sibling = elesHasLineNumber[i].previousElementSibling;
1355
+ if (curr.offsetTop + curr.offsetHeight > cMaxScrollLength && sibling.offsetTop < cMaxScrollLength) {
1356
+ lineNumer = Number(sibling.dataset.line);
1357
+ break;
1358
+ }
1359
+ }
1360
+ for (let i = blockMap.length - 1; i >= 0; i--) {
1361
+ const itemBottom = getBottomByLine(blockMap[i].end);
1362
+ const itemTop = getTopByLine(blockMap[i].start);
1363
+ if (itemBottom > pMaxScrollLength && itemTop <= pMaxScrollLength) {
1364
+ lineNumer = lineNumer < blockMap[i].start ? lineNumer : blockMap[i].start;
1365
+ break;
1366
+ }
1367
+ }
1368
+ startTop = getTopByLine(lineNumer);
1369
+ scale = (scrollDOM.scrollTop - startTop) / (pMaxScrollLength - startTop);
1370
+ startEleOffetTop = (_c = document.querySelector(`[data-line="${lineNumer}"]`)) == null ? void 0 : _c.offsetTop;
1371
+ blockHeight = cMaxScrollLength - startEleOffetTop + getComputedStyleNum(cEle, "padding-top");
1354
1372
  }
1355
1373
  const scrollToTop = startEleOffetTop - cElePaddingTop + blockHeight * scale;
1356
1374
  smoothScroll(cEle, scrollToTop, () => {
@@ -1370,7 +1388,7 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1370
1388
  let realEleEnd = (_b = cEle.firstElementChild) == null ? void 0 : _b.lastElementChild;
1371
1389
  if (startLines.length > 0) {
1372
1390
  let virtualLine = Math.ceil(
1373
- Number(startLines[startLines.length - 1]) * (cScrollTop / cScrollHeight)
1391
+ startLines[startLines.length - 1] * (cScrollTop / cScrollHeight)
1374
1392
  );
1375
1393
  let startLineIndex = startLines.findLastIndex((value) => value <= virtualLine);
1376
1394
  startLineIndex = startLineIndex === -1 ? 0 : startLineIndex;
@@ -1395,7 +1413,7 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1395
1413
  break;
1396
1414
  }
1397
1415
  }
1398
- switch (virtualLine) {
1416
+ switch (startLineIndex) {
1399
1417
  case -1: {
1400
1418
  realEleStart = (_c = cEle.firstElementChild) == null ? void 0 : _c.firstElementChild;
1401
1419
  realEleEnd = elesHasLineNumber[startLineIndex];
@@ -1417,11 +1435,13 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1417
1435
  let scale = 0;
1418
1436
  const { start, end } = blockMap[Number(realEleStart.dataset.line || 0)];
1419
1437
  const firstLineScrollTop = getTopByLine(start);
1420
- const endLineScrollTop = getTopByLine(end === view2.state.doc.lines ? end : end + 1);
1438
+ const endLineScrollTop = getTopByLine(
1439
+ end + 1 === view2.state.doc.lines ? end : end + 1
1440
+ );
1421
1441
  let blockHeight = 0;
1422
- if (endLineScrollTop > scrollDOM.scrollHeight - scrollDOM.clientHeight || realEleEnd.scrollTop > cEle.scrollHeight - cEle.clientHeight) {
1423
- scale = (cScrollTop + cEle.clientHeight - eleStartOffsetTop) / (cEle.scrollHeight - eleStartOffsetTop);
1424
- blockHeight = pEle.scrollHeight - firstLineScrollTop - pEle.clientHeight;
1442
+ if (endLineScrollTop > scrollDOM.scrollHeight - scrollDOM.clientHeight || realEleEnd.offsetTop + realEleEnd.offsetHeight > cEle.scrollHeight - cEle.clientHeight) {
1443
+ scale = (cScrollTop - eleStartOffsetTop) / (cEle.scrollHeight - eleStartOffsetTop - cEle.clientHeight);
1444
+ blockHeight = scrollDOM.scrollHeight - firstLineScrollTop - scrollDOM.clientHeight;
1425
1445
  } else if (realEleStart === ((_f = cEle.firstElementChild) == null ? void 0 : _f.firstElementChild)) {
1426
1446
  if (realEleStart === realEleEnd) {
1427
1447
  eleEndOffsetTop = realEleEnd.offsetTop + realEleEnd.offsetHeight + +getComputedStyle(realEleEnd).marginBottom.replace("px", "");
@@ -1233,6 +1233,9 @@ const ToolBar = /* @__PURE__ */ defineComponent({
1233
1233
  }
1234
1234
  });
1235
1235
  const DATA_LINE_SELECTOR = `.${prefix}-preview > [data-line]`;
1236
+ const getComputedStyleNum = (ele, key) => {
1237
+ return +getComputedStyle(ele).getPropertyValue(key).replace("px", "");
1238
+ };
1236
1239
  const scrollAutoWithScale = (pEle, cEle) => {
1237
1240
  const addEvent = debounce(() => {
1238
1241
  pEle.removeEventListener("scroll", scrollHandler);
@@ -1278,30 +1281,28 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1278
1281
  const { view } = codeMirrorUt;
1279
1282
  const smoothScroll = createSmoothScroll();
1280
1283
  const getTopByLine = (line) => {
1281
- return view.lineBlockAt(view.state.doc.line(line).from).top;
1284
+ return view.lineBlockAt(view.state.doc.line(line + 1).from).top;
1282
1285
  };
1283
- const getHeightByLine = (line) => {
1284
- return view.lineBlockAt(view.state.doc.line(line).from).height;
1286
+ const getBottomByLine = (line) => {
1287
+ return view.lineBlockAt(view.state.doc.line(line + 1).from).bottom;
1285
1288
  };
1286
1289
  let blockMap = [];
1287
1290
  let elesHasLineNumber = [];
1288
1291
  let startLines = [];
1289
1292
  const buildMap = () => {
1290
1293
  blockMap = [];
1291
- elesHasLineNumber = [];
1292
- startLines = [];
1293
- elesHasLineNumber = elesHasLineNumber.concat(
1294
- Array.from(cEle.querySelectorAll(DATA_LINE_SELECTOR))
1294
+ elesHasLineNumber = Array.from(
1295
+ cEle.querySelectorAll(DATA_LINE_SELECTOR)
1295
1296
  );
1296
- const startLinesCalc = elesHasLineNumber.map((item) => Number(item.dataset.line) + 1);
1297
- startLines = startLines.concat(startLinesCalc);
1297
+ startLines = elesHasLineNumber.map((item) => Number(item.dataset.line));
1298
+ const tempStartLines = [...startLines];
1298
1299
  const { lines } = view.state.doc;
1299
- let start = 1;
1300
- let end = startLinesCalc.shift() ?? lines;
1301
- for (let i = 1; i <= lines; i++) {
1300
+ let start = tempStartLines.shift() || 0;
1301
+ let end = tempStartLines.shift() || lines;
1302
+ for (let i = 0; i < lines; i++) {
1302
1303
  if (i === end) {
1303
1304
  start = i;
1304
- end = startLinesCalc.shift() || lines + 1;
1305
+ end = tempStartLines.shift() || lines;
1305
1306
  }
1306
1307
  blockMap.push({
1307
1308
  start,
@@ -1312,44 +1313,61 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1312
1313
  let pLock = 0;
1313
1314
  let cLock = 0;
1314
1315
  const pEleHandler = () => {
1315
- var _a, _b;
1316
+ var _a, _b, _c;
1316
1317
  if (cLock !== 0) {
1317
1318
  return false;
1318
1319
  }
1319
1320
  pLock++;
1320
1321
  const { scrollDOM, contentHeight } = view;
1321
- let cElePaddingTop = +getComputedStyle(cEle).paddingTop.replace("px", "");
1322
+ let cElePaddingTop = getComputedStyleNum(cEle, "padding-top");
1322
1323
  const blockInfo = view.lineBlockAtHeight(scrollDOM.scrollTop);
1323
1324
  const { number: currLine } = view.state.doc.lineAt(blockInfo.from);
1324
1325
  const blockData = blockMap[currLine - 1];
1325
1326
  if (!blockData) {
1326
1327
  return false;
1327
1328
  }
1328
- let endLinePosition = getTopByLine(blockData.end) + getHeightByLine(blockData.end);
1329
- let scale = 0;
1330
- const startTop = getTopByLine(blockData.start);
1331
- const startEle = cEle.querySelector(`[data-line="${blockData.start - 1}"]`) || ((_a = cEle.firstElementChild) == null ? void 0 : _a.firstElementChild);
1332
- const endEle = cEle.querySelector(`[data-line="${blockData.end}"]`) || ((_b = cEle.lastElementChild) == null ? void 0 : _b.lastElementChild);
1333
- let blockHeight = 0;
1334
- let startEleOffetTop = 0;
1329
+ let scale = 1;
1330
+ const startEle = cEle.querySelector(`[data-line="${blockData.start}"]`) || ((_a = cEle.firstElementChild) == null ? void 0 : _a.firstElementChild);
1331
+ const endEle = cEle.querySelector(`[data-line="${blockData.end + 1}"]`) || ((_b = cEle.lastElementChild) == null ? void 0 : _b.lastElementChild);
1332
+ const pMaxScrollLength = scrollDOM.scrollHeight - scrollDOM.clientHeight;
1333
+ const cMaxScrollLength = cEle.scrollHeight - cEle.clientHeight;
1334
+ let startTop = getTopByLine(blockData.start);
1335
+ let endBottom = getBottomByLine(blockData.end);
1336
+ let startEleOffetTop = startEle.offsetTop;
1337
+ let blockHeight = endEle.offsetTop - startEleOffetTop;
1335
1338
  if (startTop === 0) {
1336
- scale = scrollDOM.scrollTop / endLinePosition;
1339
+ startEleOffetTop = 0;
1337
1340
  if (startEle === endEle) {
1338
1341
  cElePaddingTop = 0;
1339
- endLinePosition = contentHeight - scrollDOM.offsetHeight;
1340
- blockHeight = endEle.offsetTop + endEle.offsetHeight - cEle.clientHeight;
1342
+ endBottom = contentHeight - scrollDOM.offsetHeight;
1343
+ blockHeight = cMaxScrollLength;
1341
1344
  } else {
1342
1345
  blockHeight = endEle.offsetTop;
1343
1346
  }
1344
- } else {
1345
- scale = (scrollDOM.scrollTop - startTop) / (endLinePosition - startTop);
1346
- startEleOffetTop = startEle.offsetTop;
1347
- blockHeight = endEle.offsetTop - startEleOffetTop;
1348
1347
  }
1349
- if (endLinePosition >= scrollDOM.scrollHeight - scrollDOM.clientHeight) {
1350
- scale = (scrollDOM.scrollTop - startTop) / (scrollDOM.scrollHeight - scrollDOM.clientHeight - startTop);
1351
- startEleOffetTop = startEle.offsetTop;
1352
- blockHeight = cEle.scrollHeight - cEle.clientHeight - startEleOffetTop + 10;
1348
+ scale = (scrollDOM.scrollTop - startTop + getComputedStyleNum(view.contentDOM, "padding-bottom")) / (endBottom - startTop);
1349
+ if (startTop > 0 && (endBottom >= pMaxScrollLength || endEle.offsetTop + endEle.clientHeight > cMaxScrollLength)) {
1350
+ let lineNumer = 0;
1351
+ for (let i = elesHasLineNumber.length - 1; i >= 0; i--) {
1352
+ const curr = elesHasLineNumber[i];
1353
+ const sibling = elesHasLineNumber[i].previousElementSibling;
1354
+ if (curr.offsetTop + curr.offsetHeight > cMaxScrollLength && sibling.offsetTop < cMaxScrollLength) {
1355
+ lineNumer = Number(sibling.dataset.line);
1356
+ break;
1357
+ }
1358
+ }
1359
+ for (let i = blockMap.length - 1; i >= 0; i--) {
1360
+ const itemBottom = getBottomByLine(blockMap[i].end);
1361
+ const itemTop = getTopByLine(blockMap[i].start);
1362
+ if (itemBottom > pMaxScrollLength && itemTop <= pMaxScrollLength) {
1363
+ lineNumer = lineNumer < blockMap[i].start ? lineNumer : blockMap[i].start;
1364
+ break;
1365
+ }
1366
+ }
1367
+ startTop = getTopByLine(lineNumer);
1368
+ scale = (scrollDOM.scrollTop - startTop) / (pMaxScrollLength - startTop);
1369
+ startEleOffetTop = (_c = document.querySelector(`[data-line="${lineNumer}"]`)) == null ? void 0 : _c.offsetTop;
1370
+ blockHeight = cMaxScrollLength - startEleOffetTop + getComputedStyleNum(cEle, "padding-top");
1353
1371
  }
1354
1372
  const scrollToTop = startEleOffetTop - cElePaddingTop + blockHeight * scale;
1355
1373
  smoothScroll(cEle, scrollToTop, () => {
@@ -1369,7 +1387,7 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1369
1387
  let realEleEnd = (_b = cEle.firstElementChild) == null ? void 0 : _b.lastElementChild;
1370
1388
  if (startLines.length > 0) {
1371
1389
  let virtualLine = Math.ceil(
1372
- Number(startLines[startLines.length - 1]) * (cScrollTop / cScrollHeight)
1390
+ startLines[startLines.length - 1] * (cScrollTop / cScrollHeight)
1373
1391
  );
1374
1392
  let startLineIndex = startLines.findLastIndex((value) => value <= virtualLine);
1375
1393
  startLineIndex = startLineIndex === -1 ? 0 : startLineIndex;
@@ -1394,7 +1412,7 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1394
1412
  break;
1395
1413
  }
1396
1414
  }
1397
- switch (virtualLine) {
1415
+ switch (startLineIndex) {
1398
1416
  case -1: {
1399
1417
  realEleStart = (_c = cEle.firstElementChild) == null ? void 0 : _c.firstElementChild;
1400
1418
  realEleEnd = elesHasLineNumber[startLineIndex];
@@ -1416,11 +1434,13 @@ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
1416
1434
  let scale = 0;
1417
1435
  const { start, end } = blockMap[Number(realEleStart.dataset.line || 0)];
1418
1436
  const firstLineScrollTop = getTopByLine(start);
1419
- const endLineScrollTop = getTopByLine(end === view.state.doc.lines ? end : end + 1);
1437
+ const endLineScrollTop = getTopByLine(
1438
+ end + 1 === view.state.doc.lines ? end : end + 1
1439
+ );
1420
1440
  let blockHeight = 0;
1421
- if (endLineScrollTop > scrollDOM.scrollHeight - scrollDOM.clientHeight || realEleEnd.scrollTop > cEle.scrollHeight - cEle.clientHeight) {
1422
- scale = (cScrollTop + cEle.clientHeight - eleStartOffsetTop) / (cEle.scrollHeight - eleStartOffsetTop);
1423
- blockHeight = pEle.scrollHeight - firstLineScrollTop - pEle.clientHeight;
1441
+ if (endLineScrollTop > scrollDOM.scrollHeight - scrollDOM.clientHeight || realEleEnd.offsetTop + realEleEnd.offsetHeight > cEle.scrollHeight - cEle.clientHeight) {
1442
+ scale = (cScrollTop - eleStartOffsetTop) / (cEle.scrollHeight - eleStartOffsetTop - cEle.clientHeight);
1443
+ blockHeight = scrollDOM.scrollHeight - firstLineScrollTop - scrollDOM.clientHeight;
1424
1444
  } else if (realEleStart === ((_f = cEle.firstElementChild) == null ? void 0 : _f.firstElementChild)) {
1425
1445
  if (realEleStart === realEleEnd) {
1426
1446
  eleEndOffsetTop = realEleEnd.offsetTop + realEleEnd.offsetHeight + +getComputedStyle(realEleEnd).marginBottom.replace("px", "");