mirror of
https://github.com/Ionaru/easy-markdown-editor
synced 2025-08-17 14:12:44 -06:00
WIP: Need more tuning to handle multi characters in pasting
This commit is contained in:
parent
696d731a47
commit
1b98dd833e
@ -2362,12 +2362,19 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ((obj.from.line === obj.to.line) && obj.text.length < 2) {
|
if ((obj.from.line === obj.to.line) && obj.text.length < 2) {
|
||||||
var myLevels, myText = cm.getRange({line: obj.from.line, ch: 0}, {line: obj.to.line, ch: 8});
|
var myLevels, myText;
|
||||||
if (/input/.test(obj.origin) && 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;
|
||||||
}
|
}
|
||||||
|
myText = cm.getRange({
|
||||||
|
line: obj.from.line,
|
||||||
|
ch: 0,
|
||||||
|
}, {
|
||||||
|
line: obj.to.line,
|
||||||
|
ch: 8,
|
||||||
|
});
|
||||||
if (!/#/.test(myText)) {
|
if (!/#/.test(myText)) {
|
||||||
myText = '# ' + myText.trim(); // Wasn't heading
|
myText = '# ' + myText.trim(); // Wasn't heading
|
||||||
} else {
|
} else {
|
||||||
@ -2389,16 +2396,49 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (/delete/.test(obj.origin) && obj.text[0] === '') {
|
else if (/delete/.test(obj.origin) && obj.text[0] === '') {
|
||||||
var myTextPart1 = myText.substring(0, obj.from.ch),
|
var delChar = cm.getRange({
|
||||||
myTextPart2 = myText.substring(obj.from.ch + 1),
|
line: obj.from.line,
|
||||||
isPart1Heading = /^#/.test(myTextPart1) ? true : false,
|
ch: obj.from.ch,
|
||||||
isPart2Heading = /^#/.test(myTextPart2) ? true : false;
|
}, {
|
||||||
if (!isPart1Heading && !isPart2Heading) {
|
line: obj.to.line,
|
||||||
|
ch: obj.to.ch,
|
||||||
|
});
|
||||||
|
if (!delChar || !delChar.length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
myText = myTextPart1 + myTextPart2;
|
var searchDir = 'asc', myStart = {
|
||||||
var delChar = cm.getRange({line: obj.from.line, ch: obj.from.ch}, {line: obj.to.line, ch: obj.to.ch}),
|
line: obj.from.line,
|
||||||
|
ch: 0,
|
||||||
|
}, myEnd;
|
||||||
|
if (delChar.length === 1) {
|
||||||
|
myText = cm.getRange({line: obj.from.line, ch: 0}, {line: obj.to.line, ch: 8});
|
||||||
|
var myTextPart1 = myText.substring(0, obj.from.ch),
|
||||||
|
myTextPart2 = myText.substring(obj.from.ch + 1),
|
||||||
|
isPart1Heading = /^#/.test(myTextPart1) ? true : false,
|
||||||
|
isPart2Heading = /^#/.test(myTextPart2) ? true : false;
|
||||||
|
if (!isPart1Heading && !isPart2Heading) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
myText = myTextPart1 + myTextPart2;
|
||||||
searchDir = delChar === '#' ? 'dsc' : 'asc';
|
searchDir = delChar === '#' ? 'dsc' : 'asc';
|
||||||
|
myEnd = {
|
||||||
|
line: obj.to.line,
|
||||||
|
ch: 8,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
myText = cm.getRange({
|
||||||
|
line: obj.from.line,
|
||||||
|
ch: 0,
|
||||||
|
}, {
|
||||||
|
line: obj.to.line,
|
||||||
|
ch: obj.to.ch + 8,
|
||||||
|
});
|
||||||
|
myText = myText.replace(delChar, '');
|
||||||
|
myEnd = {
|
||||||
|
line: obj.to.line,
|
||||||
|
ch: obj.to.ch + 8,
|
||||||
|
};
|
||||||
|
}
|
||||||
myLevels = headingNeedUpdate(myText, cm.options.backdrop.headingLevels, searchDir);
|
myLevels = headingNeedUpdate(myText, cm.options.backdrop.headingLevels, searchDir);
|
||||||
if (!myLevels || !myLevels.diff) {
|
if (!myLevels || !myLevels.diff) {
|
||||||
return false;
|
return false;
|
||||||
@ -2411,18 +2451,17 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
myLevels.to--;
|
myLevels.to--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (delChar === '#') {
|
||||||
|
myLevels.from--;
|
||||||
|
}
|
||||||
var newText = myText.replace(new RegExp('^#' + '{' + myLevels.from + '}'), obj.text[0]);
|
var newText = myText.replace(new RegExp('^#' + '{' + myLevels.from + '}'), obj.text[0]);
|
||||||
cm.doc.replaceRange(newText, {
|
// Just in case do not trim on both side, only trim the space that could remain next to the cursor
|
||||||
line: obj.from.line,
|
newText = newText.replace(/^\s*/, '');
|
||||||
ch: 0,
|
cm.doc.replaceRange(newText, myStart, myEnd);
|
||||||
}, {
|
|
||||||
line: obj.to.line,
|
|
||||||
ch: 8,
|
|
||||||
});
|
|
||||||
// Be gentle and set back the cursor at the appropriate position
|
// Be gentle and set back the cursor at the appropriate position
|
||||||
cm.doc.setCursor({
|
cm.doc.setCursor({
|
||||||
line: obj.to.line,
|
line: obj.to.line,
|
||||||
ch: obj.to.ch - myLevels.diff,
|
ch: (delChar === 1 ? obj.to.ch : obj.to.ch - 8) - myLevels.diff,
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -2449,8 +2488,13 @@ 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.origin) {
|
||||||
|
// If a modification was triggered by a code and not an human
|
||||||
|
// The origin can be "undefined", so just in case set one.
|
||||||
|
obj.origin = '+none';
|
||||||
|
}
|
||||||
if ((obj.from.line === obj.to.line) && obj.text.length < 2) {
|
if ((obj.from.line === obj.to.line) && obj.text.length < 2) {
|
||||||
if (obj.to.ch > 6) {
|
if (!/delete/.test(obj.origin) && obj.to.ch > 6) {
|
||||||
// No need to trigger a check if we are on the same line at character 7 or upper
|
// No need to trigger a check if we are on the same line at character 7 or upper
|
||||||
// As we are sure the cursor was not inside a range containing a sharp sign
|
// As we are sure the cursor was not inside a range containing a sharp sign
|
||||||
return false;
|
return false;
|
||||||
@ -2460,64 +2504,63 @@ EasyMDE.prototype.render = function (el) {
|
|||||||
obj.cancel();
|
obj.cancel();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
if (/input/.test(obj.origin)) { // Something was added
|
||||||
if (!obj.origin) {
|
if (obj.text.length === 1 && obj.text[0].length === 1) {
|
||||||
// If a modification was triggered by a code and not an human
|
// Only one character on one line is being updated
|
||||||
// The origin can be "undefined", so just in case set one.
|
if (obj.text[0] === ' ') {
|
||||||
obj.origin = '+none';
|
return headingCheckNew(cm, obj);
|
||||||
}
|
} else if (obj.text[0] === '#') {
|
||||||
if (/input/.test(obj.origin)) { // Something was added
|
return headingCheckExisting(cm, obj);
|
||||||
if (obj.text.length === 1 && obj.text[0].length === 1) {
|
}
|
||||||
// Only one character on one line is being updated
|
}
|
||||||
if (obj.text[0] === ' ') {
|
} else if (/delete/.test(obj.origin)) { // Something was removed
|
||||||
return headingCheckNew(cm, obj);
|
if (obj.text.length === 1 && !obj.text[0].length) {
|
||||||
} else if (obj.text[0] === '#') {
|
// Only one character on one line has been removed
|
||||||
return headingCheckExisting(cm, obj);
|
return headingCheckExisting(cm, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (/delete/.test(obj.origin)) { // Something was removed
|
if (!/delete/.test(obj.origin)) {
|
||||||
if (obj.text.length === 1 && !obj.text[0].length) {
|
if (obj.text.length < 2 && obj.text[0].length < 2) {
|
||||||
// Only one character on one line has been removed
|
return false;
|
||||||
return headingCheckExisting(cm, obj);
|
|
||||||
}
|
}
|
||||||
}
|
// Multilines modification like a paste
|
||||||
if (obj.text.length < 2) {
|
var r;
|
||||||
return false;
|
if (obj.from.ch === 0) {
|
||||||
}
|
// Loop each row and check for headers
|
||||||
// Multilines modification like a paste
|
for (r=0; r<obj.text.length; r++) {
|
||||||
var r;
|
|
||||||
if (obj.from.ch === 0) {
|
|
||||||
// Loop each row and check for headers
|
|
||||||
for (r=0; r<obj.text.length; r++) {
|
|
||||||
obj.text[r] = headingCheckRow(obj.text[r], cm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (obj.from.ch > 7) {
|
|
||||||
// We are sure an existing header is not involved
|
|
||||||
// So exclude the first row from the loop
|
|
||||||
for (r=1; r<obj.text.length; r++) {
|
|
||||||
obj.text[r] = headingCheckRow(obj.text[r], cm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// We need to check the first row in case an existing header is involved
|
|
||||||
var startText, oldText, newText;
|
|
||||||
for (r=0; r<obj.text.length; r++) {
|
|
||||||
if (!r) { // First row
|
|
||||||
startText = cm.getRange({line: obj.from.line, ch: 0}, {line: obj.from.line, ch: obj.from.ch});
|
|
||||||
if (/#/.test(startText)) {
|
|
||||||
oldText = startText + obj.text[r];
|
|
||||||
newText = headingCheckRow(oldText, cm);
|
|
||||||
if (oldText !== newText) { // A modification has been made
|
|
||||||
obj.text[r] = newText;
|
|
||||||
obj.from.ch = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else { // 2nd and next rows
|
|
||||||
obj.text[r] = headingCheckRow(obj.text[r], cm);
|
obj.text[r] = headingCheckRow(obj.text[r], cm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (obj.from.ch > 7) {
|
||||||
|
// We are sure an new heading is not involved with conflicting an existing one
|
||||||
|
// So we can safely exclude the first row from the loop
|
||||||
|
for (r=1; r<obj.text.length; r++) {
|
||||||
|
obj.text[r] = headingCheckRow(obj.text[r], cm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// We need to check the first row in case an existing heading exists or is updated
|
||||||
|
var startText, oldText, newText;
|
||||||
|
for (r=0; r<obj.text.length; r++) {
|
||||||
|
if (!r) { // First row
|
||||||
|
startText = cm.getRange({line: obj.from.line, ch: 0}, {line: obj.from.line, ch: obj.from.ch});
|
||||||
|
if (/#/.test(startText)) {
|
||||||
|
oldText = startText + obj.text[r];
|
||||||
|
newText = headingCheckRow(oldText, cm);
|
||||||
|
if (oldText !== newText) { // A modification has been made
|
||||||
|
obj.text[r] = newText;
|
||||||
|
obj.from.ch = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else { // 2nd and next rows
|
||||||
|
obj.text[r] = headingCheckRow(obj.text[r], cm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Multilines / multicharacters were removed
|
||||||
|
console.log( 'Fix me' );
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user