tiny-markdown-editor 0.2.1 → 0.2.3

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/lib/TinyMDE.d.ts CHANGED
@@ -42,6 +42,7 @@ export declare class Editor {
42
42
  lastCommandState: Record<string, boolean | null> | null;
43
43
  private customInlineGrammar;
44
44
  private mergedInlineGrammar;
45
+ private hasFocus;
45
46
  listeners: {
46
47
  change: EventHandler<ChangeEvent>[];
47
48
  selection: EventHandler<SelectionEvent>[];
package/lib/TinyMDE.js CHANGED
@@ -16,6 +16,7 @@ class Editor {
16
16
  this.lastCommandState = null;
17
17
  this.customInlineGrammar = {};
18
18
  this.mergedInlineGrammar = grammar_1.inlineGrammar;
19
+ this.hasFocus = true;
19
20
  this.listeners = {
20
21
  change: [],
21
22
  selection: [],
@@ -35,6 +36,7 @@ class Editor {
35
36
  this.linkLabels = [];
36
37
  this.lineDirty = [];
37
38
  this.lastCommandState = null;
39
+ this.hasFocus = true;
38
40
  this.customInlineGrammar = props.customInlineGrammar || {};
39
41
  this.mergedInlineGrammar = (0, grammar_1.createMergedInlineGrammar)(this.customInlineGrammar);
40
42
  this.listeners = {
@@ -174,7 +176,13 @@ class Editor {
174
176
  }
175
177
  this.e.addEventListener("input", (e) => this.handleInputEvent(e));
176
178
  this.e.addEventListener("compositionend", (e) => this.handleInputEvent(e));
177
- document.addEventListener("selectionchange", (e) => this.handleSelectionChangeEvent(e));
179
+ document.addEventListener("selectionchange", (e) => {
180
+ if (this.hasFocus) {
181
+ this.handleSelectionChangeEvent(e);
182
+ }
183
+ });
184
+ this.e.addEventListener("blur", () => this.hasFocus = false);
185
+ this.e.addEventListener("focus", () => this.hasFocus = true);
178
186
  this.e.addEventListener("paste", (e) => this.handlePaste(e));
179
187
  this.e.addEventListener("drop", (e) => this.handleDrop(e));
180
188
  this.lineElements = this.e.childNodes;
@@ -378,7 +386,7 @@ class Editor {
378
386
  }
379
387
  }
380
388
  getSelection(getAnchor = false) {
381
- var _a;
389
+ var _a, _b;
382
390
  const selection = window.getSelection();
383
391
  let startNode = getAnchor ? selection.anchorNode : selection.focusNode;
384
392
  if (!startNode)
@@ -403,16 +411,25 @@ class Editor {
403
411
  node = node.parentElement;
404
412
  }
405
413
  let row = 0;
406
- if (node.dataset &&
407
- node.dataset.lineNum &&
408
- (!node.previousSibling ||
409
- (((_a = node.previousSibling.dataset) === null || _a === void 0 ? void 0 : _a.lineNum) !== node.dataset.lineNum))) {
410
- row = parseInt(node.dataset.lineNum);
411
- }
412
- else {
413
- while (node.previousSibling) {
414
- row++;
415
- node = node.previousSibling;
414
+ // If the node doesn't have a previous sibling, it must be the first line
415
+ if (node.previousSibling) {
416
+ const currentLineNumData = (_a = node.dataset) === null || _a === void 0 ? void 0 : _a.lineNum;
417
+ const previousLineNumData = (_b = node.previousSibling.dataset) === null || _b === void 0 ? void 0 : _b.lineNum;
418
+ if (currentLineNumData && previousLineNumData) {
419
+ const currentLineNum = parseInt(currentLineNumData);
420
+ const previousLineNum = parseInt(previousLineNumData);
421
+ if (currentLineNum === previousLineNum + 1) {
422
+ row = currentLineNum;
423
+ }
424
+ else {
425
+ // If the current line is NOT the previous line + 1, then either
426
+ // the current line got split in two or merged with the previous line
427
+ // Either way, we need to recalculate the row number
428
+ while (node.previousSibling) {
429
+ row++;
430
+ node = node.previousSibling;
431
+ }
432
+ }
416
433
  }
417
434
  }
418
435
  return { row: row, col: col };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-markdown-editor",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "TinyMDE: A tiny, ultra low dependency, embeddable HTML/JavaScript Markdown editor.",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -15,7 +15,11 @@
15
15
  "editor"
16
16
  ],
17
17
  "scripts": {
18
- "test": "npx gulp test",
18
+ "test": "npx gulp && npx jest",
19
+ "test:chromium": "npx gulp && npx jest --selectProjects chromium",
20
+ "test:firefox": "npx gulp && npx jest --selectProjects firefox",
21
+ "test:webkit": "npx gulp && npx jest --selectProjects webkit",
22
+ "test:single": "npx gulp test",
19
23
  "build": "npx gulp",
20
24
  "dev": "npx gulp dev",
21
25
  "typecheck": "npx tsc --noEmit",
@@ -44,19 +48,20 @@
44
48
  "@babel/preset-env": "^7.27.2",
45
49
  "@babel/preset-typescript": "^7.27.1",
46
50
  "@octokit/rest": "^22.0.0",
51
+ "@playwright/test": "^1.54.1",
47
52
  "@rollup/plugin-babel": "^6.0.4",
48
53
  "@rollup/plugin-commonjs": "^28.0.3",
49
54
  "@rollup/plugin-node-resolve": "^16.0.1",
50
55
  "@rollup/plugin-typescript": "^12.1.1",
51
56
  "@rollup/stream": "^3.0.1",
52
57
  "@types/jest": "^29.5.14",
58
+ "@typescript-eslint/eslint-plugin": "^8.20.0",
59
+ "@typescript-eslint/parser": "^8.20.0",
53
60
  "autoprefixer": "^10.4.21",
54
61
  "cssnano": "^7.0.7",
55
62
  "del": "^8.0.0",
56
63
  "dotenv": "^16.5.0",
57
64
  "eslint": "^9.28.0",
58
- "@typescript-eslint/eslint-plugin": "^8.20.0",
59
- "@typescript-eslint/parser": "^8.20.0",
60
65
  "express": "^5.1.0",
61
66
  "gulp": "^5.0.1",
62
67
  "gulp-babel": "^8.0.0",
@@ -67,9 +72,8 @@
67
72
  "gulp-typescript": "^6.0.0-alpha.1",
68
73
  "jest": "^29.7.0",
69
74
  "jest-cli": "^29.7.0",
70
- "jest-puppeteer": "^11.0.0",
75
+ "playwright": "^1.54.1",
71
76
  "postcss-import": "^16.1.0",
72
- "puppeteer": "^24.10.0",
73
77
  "rollup": "^4.41.1",
74
78
  "rollup-plugin-eslint": "^7.0.0",
75
79
  "terser": "^5.40.0",