mirror of
https://github.com/Ionaru/easy-markdown-editor
synced 2025-06-27 13:11:01 -06:00
WIP: Selective header levels filter working with single line / character input
This commit is contained in:
parent
9f93fc115c
commit
93c60bcf28
@ -2215,29 +2215,33 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
level += '#';
|
level += '#';
|
||||||
from++;
|
from++;
|
||||||
}
|
}
|
||||||
level += ' '; console.log(level);
|
level += ' ';
|
||||||
return /#/.test(headline) ? headline.replace(/#\s*/, level) : level;
|
return /#/.test(headline) ? headline.replace(/#\s*/, level) : level;
|
||||||
}
|
};
|
||||||
var headlineNeedUpdate = function(myCurrHeadline, allowedHeadlingLevels) {
|
var headlineNeedUpdate = function(myCurrHeadline, allowedHeadlingLevels, levelSearchDir) {
|
||||||
if (!myCurrHeadline || !allowedHeadlingLevels) {
|
if (!myCurrHeadline || !allowedHeadlingLevels) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!/^[#]+/.test(myCurrHeadline.trim())){
|
myCurrHeadline = myCurrHeadline.trim();
|
||||||
|
if (!/^#+/.test(myCurrHeadline)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
var currHeadlingLevel = ((myCurrHeadline || '').match(/#/g) || []).length;
|
var currHeadlingLevel = (myCurrHeadline.match(/^([#]+)/g) || [''])[0].length;
|
||||||
if (allowedHeadlingLevels.indexOf(currHeadlingLevel) !== -1) {
|
if (allowedHeadlingLevels.indexOf(currHeadlingLevel) !== -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
var newHeadingLevel = 0, n = 0;
|
levelSearchDir = levelSearchDir || 'asc';
|
||||||
while (n < allowedHeadlingLevels.length) {
|
var newHeadingLevel = -1, n = 0;
|
||||||
if (allowedHeadlingLevels[n] > currHeadlingLevel) {
|
if (levelSearchDir === 'asc') {
|
||||||
newHeadingLevel = allowedHeadlingLevels[n];
|
while (n < allowedHeadlingLevels.length) {
|
||||||
break;
|
if (allowedHeadlingLevels[n] > currHeadlingLevel) {
|
||||||
|
newHeadingLevel = allowedHeadlingLevels[n];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
n++;
|
||||||
}
|
}
|
||||||
n++;
|
|
||||||
}
|
}
|
||||||
if (!newHeadingLevel) {
|
if (newHeadingLevel < 0) {
|
||||||
n = allowedHeadlingLevels.length - 1;
|
n = allowedHeadlingLevels.length - 1;
|
||||||
while (n > -1) {
|
while (n > -1) {
|
||||||
if (allowedHeadlingLevels[n] < currHeadlingLevel) {
|
if (allowedHeadlingLevels[n] < currHeadlingLevel) {
|
||||||
@ -2246,13 +2250,20 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
}
|
}
|
||||||
n--;
|
n--;
|
||||||
}
|
}
|
||||||
|
if (levelSearchDir === 'dsc') {
|
||||||
|
currHeadlingLevel += 1;
|
||||||
|
if (newHeadingLevel < 0) {
|
||||||
|
newHeadingLevel = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!newHeadingLevel || newHeadingLevel === currHeadlingLevel) {
|
if (newHeadingLevel < 0 || newHeadingLevel === currHeadlingLevel) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
from: currHeadlingLevel,
|
from: currHeadlingLevel,
|
||||||
to: newHeadingLevel,
|
to: newHeadingLevel,
|
||||||
|
diff: Math.abs(newHeadingLevel - currHeadlingLevel),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
var checkNewHeadline = function(cm, obj) {
|
var checkNewHeadline = function(cm, obj) {
|
||||||
@ -2271,12 +2282,12 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
// Most simple case when a modification has occured on a single line
|
// Most simple case when a modification has occured on a single line
|
||||||
if (levels.to > levels.from) {
|
if (levels.to > levels.from) {
|
||||||
// Current level is forbidden so we jump to the closest upper level allowed
|
// Current level is forbidden so we jump to the closest upper level allowed
|
||||||
// We only need to update the text value before the modification is applied
|
// We only need to reset the sharp numbers with the appropriate value before the modification is applied
|
||||||
obj.text[0] = makeBiggerHeadline('', levels.from, levels.to);
|
obj.text[0] = makeBiggerHeadline('', levels.from, levels.to);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// The current level is forbidden and we jump to the closest lower level available
|
// The current level is forbidden and we jump to the closest lower level available
|
||||||
// A bit more complicated: we have to cancel the update and trigger a replacement with the existing string
|
// A bit more is needed: we have to cancel the requested update and trigger a replacement with the existing sharp signs
|
||||||
obj.cancel();
|
obj.cancel();
|
||||||
var newHeadline = '';
|
var newHeadline = '';
|
||||||
while (levels.to > 0) {
|
while (levels.to > 0) {
|
||||||
@ -2290,7 +2301,7 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
}, {
|
}, {
|
||||||
line: obj.to.line,
|
line: obj.to.line,
|
||||||
ch: obj.to.ch,
|
ch: obj.to.ch,
|
||||||
}, myHeadline );
|
}, myHeadline ); // 4th arguments to keep a trace in the history when possible
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -2303,27 +2314,66 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
line: obj.to.line,
|
line: obj.to.line,
|
||||||
ch: obj.to.ch + 1,
|
ch: obj.to.ch + 1,
|
||||||
});
|
});
|
||||||
console.log( '==' + myChar + '==' );
|
// console.log( '==' + myChar + '==' );
|
||||||
if (!/\s|#/.test(myChar || '')) {
|
if (!/\s|#/.test(myChar || '')) {
|
||||||
|
// Don't bother to go further if no headlines were detected
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
var myText = cm.getRange({line: obj.from.line, ch: 0}, {line: obj.to.line, ch: 8});
|
if ((obj.from.line === obj.to.line) && obj.text.length === 1) {
|
||||||
console.log( myText );
|
var myLevels, myText = cm.getRange({line: obj.from.line, ch: 0}, {line: obj.to.line, ch: 8});
|
||||||
if ((obj.from.line === obj.to.line) && obj.text.length === 1 && obj.text[0] === '#') {
|
if (/input/.test(obj.origin) && obj.text[0] === '#') {
|
||||||
if (!/[^\s#]/.test(myText)) {
|
if (!/[^\s#]/.test(myText)) {
|
||||||
// Newly created, skip the check for now
|
// Newly created, skip the check for now
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
var levels = headlineNeedUpdate(myText.replace(/#/, '##'), cm.options.backdrop.headingLevels);
|
|
||||||
if (!levels || !levels.from || !levels.to) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (levels.to > levels.from) {
|
|
||||||
obj.text[0] = '#';
|
|
||||||
while (levels.from < levels.to) {
|
|
||||||
obj.text[0] += '#';
|
|
||||||
levels.from++;
|
|
||||||
}
|
}
|
||||||
|
if (!/#/.test(myText)) {
|
||||||
|
myText = '# ' + myText.trim(); // Wasn't headline
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
myText = myText.replace(/#/, '##'); // Increment one sharp sign
|
||||||
|
};
|
||||||
|
myLevels = headlineNeedUpdate(myText, cm.options.backdrop.headingLevels);
|
||||||
|
if (!myLevels) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (myLevels.to < myLevels.from) {
|
||||||
|
obj.cancel();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
obj.text[0] = '#';
|
||||||
|
while (myLevels.from < myLevels.to) {
|
||||||
|
obj.text[0] += '#';
|
||||||
|
myLevels.from++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (/delete/.test(obj.origin) && obj.text[0] === '') {
|
||||||
|
myLevels = headlineNeedUpdate(myText.replace(/#/, ''), cm.options.backdrop.headingLevels, 'dsc');
|
||||||
|
if (!myLevels) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
obj.cancel();
|
||||||
|
if (myLevels.to > myLevels.from) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
obj.text[0] = '';
|
||||||
|
while (myLevels.to > 0) {
|
||||||
|
obj.text[0] += '#';
|
||||||
|
myLevels.to--;
|
||||||
|
}
|
||||||
|
var newText = myText.replace(new RegExp('^#' + '{' + myLevels.from + '}'), obj.text[0]);
|
||||||
|
cm.doc.replaceRange(newText.replace(/^\s*/, ''), {
|
||||||
|
line: obj.from.line,
|
||||||
|
ch: 0,
|
||||||
|
}, {
|
||||||
|
line: obj.to.line,
|
||||||
|
ch: 8,
|
||||||
|
});
|
||||||
|
// Be gentle and set back the cursor at the appropriate position
|
||||||
|
cm.doc.setCursor({
|
||||||
|
line: obj.to.line,
|
||||||
|
ch: obj.to.ch - myLevels.diff,
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2334,18 +2384,29 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
// Don't go further 'cause a required argument is missing...
|
// Don't go further 'cause a required argument is missing...
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ((obj.from.line === obj.to.line) && obj.to.ch > 6) {
|
if ((obj.from.line === obj.to.line) ) {
|
||||||
// No need to trigger a check if we are on the same line at character 7 or upper
|
if (obj.to.ch > 6) {
|
||||||
// As we are sure the cursor is not inside a markdown header
|
// No need to trigger a check if we are on the same line at character 7 or upper
|
||||||
return false;
|
// As we are sure the cursor is not inside a range containing a sharp sign
|
||||||
}
|
|
||||||
if (/input/.test(obj.origin || '+input')) { // Something was added
|
|
||||||
if (!obj.text.length || !obj.text[0].length) {
|
|
||||||
// Just in case
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (obj.from.ch === 0 && obj.to.ch === 0 && /\s|\b/.test(obj.text[0] || '')) {
|
||||||
|
// Prevent space at the beginning of the line
|
||||||
|
obj.cancel();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!obj.text.length) {
|
||||||
|
// Just in case
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!obj.origin) {
|
||||||
|
// Just in case
|
||||||
|
obj.origin = '+none';
|
||||||
|
}
|
||||||
|
if (/input/.test(obj.origin)) { // Something was added
|
||||||
if (obj.text.length === 1 && obj.text[0].length === 1) {
|
if (obj.text.length === 1 && obj.text[0].length === 1) {
|
||||||
// Only one character in one line is being updated
|
// Only one character on one line is being updated
|
||||||
if (obj.text[0] === ' ') {
|
if (obj.text[0] === ' ') {
|
||||||
return checkNewHeadline(cm, obj);
|
return checkNewHeadline(cm, obj);
|
||||||
}
|
}
|
||||||
@ -2356,12 +2417,12 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (/delete/.test(obj.origin)) { // Something was removed
|
else if (/delete/.test(obj.origin)) { // Something was removed
|
||||||
return checkExistingHeadline(cm, obj);
|
if (obj.text.length === 1 && !obj.text[0].length) {
|
||||||
|
// Only one character on one line has been removed
|
||||||
|
return checkExistingHeadline(cm, obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user