-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtoggleCheckbox.ts
141 lines (125 loc) · 4.49 KB
/
toggleCheckbox.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as vscode from 'vscode';
import { Position, Range, TextEditorEdit } from 'vscode';
import * as helpers from './helpers';
const moment = require('moment');
/** Mark a checkbox as checked or unchecked */
export const toggleCheckbox = async () => {
// the position object gives you the line and character where the cursor is
const editor = helpers.getEditor();
if (editor.selection.isEmpty) {
const cursorPosition = helpers.getCursorPosition();
const line = editor.document.lineAt(cursorPosition.line);
await toggleCheckboxOfLine(line);
const endLine = editor.document.lineAt(editor.selection.end.line);
const selectionPosition = new vscode.Position(endLine.lineNumber, 20000);
helpers.getEditor().selection = new vscode.Selection(
selectionPosition,
selectionPosition
);
} else {
const selection = editor.selection;
// get all line numbers of the selection
for (let r = selection.start.line; r <= selection.end.line; r++) {
const line = editor.document.lineAt(r);
await toggleCheckboxOfLine(line);
}
}
};
/** mark or unmark the checkbox of a given line in the editor */
export const toggleCheckboxOfLine = (
line: vscode.TextLine,
checkIt?: boolean
) => {
const checkbox = helpers.getCheckboxOfLine(line);
// no action required
if (
!checkbox ||
(!checkbox.checked && checkIt === false) ||
(checkbox.checked === true && checkIt === true)
) {
return Promise.resolve(undefined);
}
let value = ' ';
// if the checkbox is not checked or it must be checked
if (checkIt === true || (checkIt === undefined && !checkbox.checked)) {
value = helpers.getConfig<string>('checkmark');
}
return markField(checkbox.position, value);
};
/** Marks the field inside the checkbox with a character */
const markField = (
checkboxPosition: Position,
replacement: string
): Thenable<boolean> => {
const editor = helpers.getEditor();
const checkmark = helpers.getConfig<string>('checkmark');
return editor.edit((editBuilder: TextEditorEdit) => {
editBuilder.replace(
new Range(
new Position(checkboxPosition.line, checkboxPosition.character + 1),
new Position(
checkboxPosition.line,
checkboxPosition.character +
(replacement !== ' ' ? 2 : checkmark.length + 1)
)
),
replacement
);
// get settings from config
const italicWhenChecked = helpers.getConfig<boolean>('italicWhenChecked');
const strikeThroughWhenChecked = helpers.getConfig<boolean>(
'strikeThroughWhenChecked'
);
const dateWhenChecked = helpers.getConfig<boolean>('dateWhenChecked');
const dateFormat = helpers.getConfig<string>('dateFormat');
// get line of the checkbox
const line = editor.document.lineAt(checkboxPosition.line);
const lhc = helpers.getCheckboxOfLine(line);
const lineText = line.text;
const textWithoutCheckbox = lineText
.substr(checkboxPosition.character + 4, lineText.length)
.trim();
// respect trailing whitespace
const foundTrailingWhitespace = lineText
.substr(checkboxPosition.character + 4, lineText.length)
.match(/[\s\n\r]*$/);
const whitespace = foundTrailingWhitespace?.join('') || '';
if (lhc && !lhc.checked && textWithoutCheckbox.length > 0) {
let newText = textWithoutCheckbox;
// apply different formats to highlight checked status
if (italicWhenChecked) {
newText = `*${newText}*`;
}
if (strikeThroughWhenChecked) {
newText = `~~${newText}~~`;
}
if (dateWhenChecked) {
const date = moment(new Date()).format(dateFormat);
const decoratedDate = helpers
.getConfig<string>('dateTemplate')
.replace('{date}', date);
newText = `${newText} ${decoratedDate}${whitespace}`;
}
editBuilder.replace(
new Range(
new Position(checkboxPosition.line, checkboxPosition.character + 4),
new Position(checkboxPosition.line, line.text.length)
),
newText
);
} else if (lhc && lhc.checked) {
let newText = textWithoutCheckbox.replace(/~~/g, '').replace(/\*/g, '');
// remove the date string
if (dateWhenChecked) {
newText = newText.replace(/\s+\[[^[]+?\]$/, '') + whitespace;
}
editBuilder.replace(
new Range(
new Position(checkboxPosition.line, checkboxPosition.character + 4),
new Position(checkboxPosition.line, line.text.length)
),
newText
);
}
});
};