Skip to content

Commit 4f8f66f

Browse files
committed
Bugfixes
1 parent 367ac24 commit 4f8f66f

2 files changed

Lines changed: 118 additions & 4 deletions

File tree

projects/markular/src/lib/markular.component.spec.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,43 @@ describe('Markular', () => {
3838
expect(component._val).toBe('# Hello');
3939
});
4040

41+
it('should insert H1 when subselect', () => {
42+
component._val = 'Hello';
43+
component.selStart = 2;
44+
component.selEnd = 3;
45+
component.toggleHeading(1);
46+
expect(component._val).toBe('He# llo');
47+
});
48+
49+
it('should insert H1 when none selected', () => {
50+
component._val = 'Hello';
51+
component.selStart = 3;
52+
component.selEnd = 3;
53+
component.toggleHeading(1);
54+
expect(component._val).toBe('# Hello');
55+
});
56+
4157
it('should be H1', () => {
4258
component._val = '# Hello';
4359
component.selStart = 0;
4460
component.selEnd = 7;
4561
expect(component.isHeading(1)).toBeTruthy();
4662
});
4763

64+
it('should be H1 when none selected', () => {
65+
component._val = '# Hello';
66+
component.selStart = 5;
67+
component.selEnd = 5;
68+
expect(component.isHeading(1)).toBeTruthy();
69+
});
70+
71+
it('should not be H1 when subselect', () => {
72+
component._val = '# Hello';
73+
component.selStart = 3;
74+
component.selEnd = 5;
75+
expect(component.isHeading(1)).toBeFalsy();
76+
});
77+
4878
it('should remove H1', () => {
4979
component._val = '# Hello';
5080
component.selStart = 0;
@@ -136,6 +166,22 @@ describe('Markular', () => {
136166
expect(component._val).toBe('- Hello');
137167
});
138168

169+
it('should insert ul when subselect', () => {
170+
component._val = 'Hello';
171+
component.selStart = 2;
172+
component.selEnd = 3;
173+
component.toggleUnorderedList();
174+
expect(component._val).toBe('He- llo');
175+
});
176+
177+
it('should insert ul when none selected', () => {
178+
component._val = 'Hello';
179+
component.selStart = 3;
180+
component.selEnd = 3;
181+
component.toggleUnorderedList();
182+
expect(component._val).toBe('- Hello');
183+
});
184+
139185
it('should insert ul multiline', () => {
140186
component._val = 'Hello\nWorld';
141187
component.selStart = 0;
@@ -151,13 +197,27 @@ describe('Markular', () => {
151197
expect(component.isUnorderedList()).toBeTruthy();
152198
});
153199

200+
it('should be ul none selected', () => {
201+
component._val = '- Hello';
202+
component.selStart = 6;
203+
component.selEnd = 6;
204+
expect(component.isUnorderedList()).toBeTruthy();
205+
});
206+
154207
it('should be ul multiline', () => {
155208
component._val = '- Hello\n- World';
156209
component.selStart = 0;
157210
component.selEnd = 11;
158211
expect(component.isUnorderedList()).toBeTruthy();
159212
});
160213

214+
it('should not be ul when subselect', () => {
215+
component._val = '- Hello';
216+
component.selStart = 4;
217+
component.selEnd = 6;
218+
expect(component.isUnorderedList()).toBeFalsy();
219+
});
220+
161221
it('should remove ul', () => {
162222
component._val = '- Hello';
163223
component.selStart = 0;
@@ -208,6 +268,22 @@ describe('Markular', () => {
208268
expect(component._val).toBe('1. Hello');
209269
});
210270

271+
it('should insert ol when subselect', () => {
272+
component._val = 'Hello';
273+
component.selStart = 2;
274+
component.selEnd = 3;
275+
component.toggleOrderedList();
276+
expect(component._val).toBe('He1. llo');
277+
});
278+
279+
it('should insert ol when none selected', () => {
280+
component._val = 'Hello';
281+
component.selStart = 3;
282+
component.selEnd = 3;
283+
component.toggleOrderedList();
284+
expect(component._val).toBe('1. Hello');
285+
});
286+
211287
it('should insert ol multiline', () => {
212288
component._val = 'Hello\nWorld';
213289
component.selStart = 0;
@@ -223,13 +299,27 @@ describe('Markular', () => {
223299
expect(component.isOrderedList()).toBeTruthy();
224300
});
225301

302+
it('should be ol none selected', () => {
303+
component._val = '1. Hello';
304+
component.selStart = 6;
305+
component.selEnd = 6;
306+
expect(component.isOrderedList()).toBeTruthy();
307+
});
308+
226309
it('should be ol multiline', () => {
227310
component._val = '1. Hello\n2. World';
228311
component.selStart = 0;
229312
component.selEnd = 13;
230313
expect(component.isOrderedList()).toBeTruthy();
231314
});
232315

316+
it('should not be ol when subselect', () => {
317+
component._val = '1. Hello';
318+
component.selStart = 4;
319+
component.selEnd = 6;
320+
expect(component.isOrderedList()).toBeFalsy();
321+
});
322+
233323
it('should remove ol', () => {
234324
component._val = '1. Hello';
235325
component.selStart = 0;

projects/markular/src/lib/markular.component.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
input,
1010
InputSignal,
1111
output,
12-
ViewChild,
12+
ViewChild
1313
} from '@angular/core';
1414
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
1515
import { marked } from 'marked';
@@ -79,6 +79,28 @@ export class Markular implements AfterViewInit, ControlValueAccessor {
7979
return this._val.slice(this.selStart, this.selEnd) || '';
8080
}
8181

82+
private get currentLine(): string {
83+
const lines = this.lines;
84+
if (lines.currentLineIdx >= 0) {
85+
return (
86+
this._val.slice(
87+
lines.lines[lines.currentLineIdx].from,
88+
lines.lines[lines.currentLineIdx].to,
89+
) || ''
90+
);
91+
}
92+
93+
return '';
94+
}
95+
96+
private get selectionOrCurrentLine(): string {
97+
if (this.isNoneSelected()) {
98+
return this.currentLine;
99+
}
100+
101+
return this.selection;
102+
}
103+
82104
private get lines() {
83105
const lines = this._val.split('\n');
84106
const lineRanges = [];
@@ -221,7 +243,7 @@ export class Markular implements AfterViewInit, ControlValueAccessor {
221243

222244
isHeading(size: number): boolean {
223245
const regex = new RegExp(`^#{${size}}(?!#)`);
224-
return regex.test(this.selection);
246+
return regex.test(this.selectionOrCurrentLine);
225247
}
226248

227249
toggleHeading(size: number) {
@@ -268,7 +290,9 @@ export class Markular implements AfterViewInit, ControlValueAccessor {
268290
}
269291

270292
isUnorderedList(): boolean {
271-
return this.selection.split('\n').every((line) => /^-(?!-)/.test(line.trimStart()));
293+
return this.selectionOrCurrentLine
294+
.split('\n')
295+
.every((line) => /^-(?!-)/.test(line.trimStart()));
272296
}
273297

274298
toggleUnorderedList() {
@@ -296,7 +320,7 @@ export class Markular implements AfterViewInit, ControlValueAccessor {
296320
}
297321

298322
isOrderedList(): boolean {
299-
return this.selection.split('\n').every((line) => /^\d+\./.test(line.trimStart()));
323+
return this.selectionOrCurrentLine.split('\n').every((line) => /^\d+\./.test(line.trimStart()));
300324
}
301325

302326
toggleOrderedList() {

0 commit comments

Comments
 (0)