Skip to content

Commit 01217c9

Browse files
committed
Added extraTags support for list, and can toggle todo list in the view mode
1 parent c2f9970 commit 01217c9

2 files changed

Lines changed: 87 additions & 25 deletions

File tree

public/js/extra.js

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function slugifyWithUTF8(text) {
5050

5151
var viewAjaxCallback = null;
5252

53-
//regex for blockquote
53+
//regex for extra tags
5454
var spaceregex = /\s*/;
5555
var notinhtmltagregex = /(?![^<]*>|[^<>]*<\/)/;
5656
var coloregex = /\[color=([#|\(|\)|\s|\,|\w]*?)\]/;
@@ -61,8 +61,44 @@ var nameandtimeregex = new RegExp(nameregex.source + spaceregex.source + timereg
6161
nameregex = new RegExp(nameregex.source + notinhtmltagregex.source, "g");
6262
timeregex = new RegExp(timeregex.source + notinhtmltagregex.source, "g");
6363

64+
function replaceExtraTags(html) {
65+
html = html.replace(coloregex, '<span class="color" data-color="$1"></span>');
66+
html = html.replace(nameandtimeregex, '<small><i class="fa fa-user"></i> $1 <i class="fa fa-clock-o"></i> $2</small>');
67+
html = html.replace(nameregex, '<small><i class="fa fa-user"></i> $1</small>');
68+
html = html.replace(timeregex, '<small><i class="fa fa-clock-o"></i> $1</small>');
69+
return html;
70+
}
71+
6472
//dynamic event or object binding here
6573
function finishView(view) {
74+
//todo list
75+
var lis = view.find('li.raw').removeClass("raw").sortByDepth().toArray();
76+
for (var i = 0; i < lis.length; i++) {
77+
var li = lis[i];
78+
var html = $(li).clone()[0].innerHTML;
79+
var p = $(li).children('p');
80+
if (p.length == 1) {
81+
html = p.html();
82+
li = p[0];
83+
}
84+
html = replaceExtraTags(html);
85+
li.innerHTML = html;
86+
var disabled = 'disabled';
87+
if(typeof editor !== 'undefined' && havePermission())
88+
disabled = '';
89+
if (/^\s*\[[x ]\]\s*/.test(html)) {
90+
li.innerHTML = html.replace(/^\s*\[ \]\s*/, '<input type="checkbox" class="task-list-item-checkbox "' + disabled + '><label></label>')
91+
.replace(/^\s*\[x\]\s*/, '<input type="checkbox" class="task-list-item-checkbox" checked ' + disabled + '><label></label>');
92+
lis[i].setAttribute('class', 'task-list-item');
93+
}
94+
if (typeof editor !== 'undefined' && havePermission())
95+
$(li).find('input').change(toggleTodoEvent);
96+
//color tag in list will convert it to tag icon with color
97+
var tag_color = $(li).closest('ul').find(".color");
98+
tag_color.each(function (key, value) {
99+
$(value).addClass('fa fa-tag').css('color', $(value).attr('data-color'));
100+
});
101+
}
66102
//youtube
67103
view.find(".youtube.raw").removeClass("raw")
68104
.click(function () {
@@ -149,12 +185,10 @@ function finishView(view) {
149185
var blockquote_p = blockquote.find("p");
150186
blockquote_p.each(function (key, value) {
151187
var html = $(value).html();
152-
html = html.replace(coloregex, '<span class="color" data-color="$1"></span>');
153-
html = html.replace(nameandtimeregex, '<small><i class="fa fa-user"></i> $1 <i class="fa fa-clock-o"></i> $2</small>');
154-
html = html.replace(nameregex, '<small><i class="fa fa-user"></i> $1</small>');
155-
html = html.replace(timeregex, '<small><i class="fa fa-clock-o"></i> $1</small>');
188+
html = replaceExtraTags(html);
156189
$(value).html(html);
157190
});
191+
//color tag in blockquote will change its left border color
158192
var blockquote_color = blockquote.find(".color");
159193
blockquote_color.each(function (key, value) {
160194
$(value).closest("blockquote").css('border-left-color', $(value).attr('data-color'));
@@ -173,23 +207,8 @@ function postProcess(code) {
173207
result.find("iframe").replaceWith(function () {
174208
return "<noiframe>" + $(this).html() + "</noiframe>"
175209
});
176-
//todo list
177-
var lis = result.find('li.raw').removeClass("raw").sortByDepth().toArray();
178-
for (var i = 0; i < lis.length; i++) {
179-
var li = lis[i];
180-
var html = $(li).clone()[0].innerHTML;
181-
var p = $(li).children('p');
182-
if (p.length == 1) {
183-
html = p.html();
184-
li = p[0];
185210
}
186-
if (/^\s*\[[x ]\]\s*/.test(html)) {
187-
li.innerHTML = html.replace(/^\s*\[ \]\s*/, '<input type="checkbox" class="task-list-item-checkbox" disabled><label></label>')
188-
.replace(/^\s*\[x\]\s*/, '<input type="checkbox" class="task-list-item-checkbox" checked disabled><label></label>');
189-
lis[i].setAttribute('class', 'task-list-item');
190211
}
191-
}
192-
return result;
193212
}
194213

195214
//jQuery sortByDepth
@@ -211,6 +230,27 @@ $.fn.sortByDepth = function () {
211230
return $(result);
212231
};
213232

233+
function toggleTodoEvent(e) {
234+
var startline = $(this).closest('li').attr('data-startline') - 1;
235+
var line = editor.getLine(startline);
236+
var matches = line.match(/^[>\s]*[\-\+\*]\s\[([x ])\]/);
237+
if (matches && matches.length >= 2) {
238+
var checked = null;
239+
if (matches[1] == 'x')
240+
checked = true;
241+
else if (matches[1] == ' ')
242+
checked = false;
243+
var replacements = matches[0].match(/(^[>\s]*[\-\+\*]\s\[)([x ])(\])/);
244+
editor.replaceRange(checked ? ' ' : 'x', {
245+
line: startline,
246+
ch: replacements[1].length
247+
}, {
248+
line: startline,
249+
ch: replacements[1].length + 1
250+
}, '+input');
251+
}
252+
}
253+
214254
//remove hash
215255
function removeHash() {
216256
history.pushState("", document.title, window.location.pathname + window.location.search);

public/js/index.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ var supportExternals = [
102102
search: 'gist'
103103
}
104104
];
105-
var supportBlockquoteTags = [
105+
var supportExtraTags = [
106106
{
107107
text: '[name tag]',
108108
search: '[]',
@@ -2031,13 +2031,35 @@ $(editor.getInputField())
20312031
return !isInCode;
20322032
}
20332033
},
2034-
{ //blockquote personal info & general info
2034+
{ //extra tags for blockquote
20352035
match: /(?:^|\n|\s)(\>.*)(\[\])(\w*)$/,
20362036
search: function (term, callback) {
20372037
var list = [];
2038-
$.map(supportBlockquoteTags, function (blockquotetag) {
2039-
if (blockquotetag.search.indexOf(term) === 0)
2040-
list.push(blockquotetag.command());
2038+
$.map(supportExtraTags, function (extratag) {
2039+
if (extratag.search.indexOf(term) === 0)
2040+
list.push(extratag.command());
2041+
});
2042+
$.map(supportReferrals, function (referral) {
2043+
if (referral.search.indexOf(term) === 0)
2044+
list.push(referral.text);
2045+
})
2046+
callback(list);
2047+
checkCursorMenu();
2048+
},
2049+
replace: function (value) {
2050+
return '$1' + value;
2051+
},
2052+
context: function (text) {
2053+
return !isInCode;
2054+
}
2055+
},
2056+
{ //extra tags for list
2057+
match: /(^[>\s]*[\-\+\*]\s(?:\[[x ]\]|.*))(\[\])(\w*)$/,
2058+
search: function (term, callback) {
2059+
var list = [];
2060+
$.map(supportExtraTags, function (extratag) {
2061+
if (extratag.search.indexOf(term) === 0)
2062+
list.push(extratag.command());
20412063
});
20422064
$.map(supportReferrals, function (referral) {
20432065
if (referral.search.indexOf(term) === 0)

0 commit comments

Comments
 (0)