mirror of
https://github.com/Ionaru/easy-markdown-editor
synced 2025-09-24 16:40:55 -06:00
Fix for hyperlink doubling
Click on link multiple times no longer doubles the output, instead it now toggles the link on and off. Same functioanlity was applied to image link.
This commit is contained in:
parent
786c5b63c6
commit
3bb29f37f3
@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
<!--## [Unreleased]-->
|
<!--## [Unreleased]-->
|
||||||
|
## Fixed
|
||||||
|
- Hyperlink text getting doubled each time click on link ([#25])
|
||||||
|
|
||||||
## [2.6.1] - 2019-06-17
|
## [2.6.1] - 2019-06-17
|
||||||
### Fixed
|
### Fixed
|
||||||
- Error when toggling between ordered and unordered lists (Thanks to [@roryok], [#93]).
|
- Error when toggling between ordered and unordered lists (Thanks to [@roryok], [#93]).
|
||||||
|
@ -225,9 +225,9 @@ function getState(cm, pos) {
|
|||||||
ret.strikethrough = true;
|
ret.strikethrough = true;
|
||||||
} else if (data === 'comment') {
|
} else if (data === 'comment') {
|
||||||
ret.code = true;
|
ret.code = true;
|
||||||
} else if (data === 'link') {
|
} else if (data === 'link' && !ret.image) {
|
||||||
ret.link = true;
|
ret.link = true;
|
||||||
} else if (data === 'tag') {
|
} else if (data === 'image') {
|
||||||
ret.image = true;
|
ret.image = true;
|
||||||
} else if (data.match(/^header(-[1-6])?$/)) {
|
} else if (data.match(/^header(-[1-6])?$/)) {
|
||||||
ret[data.replace('header', 'heading')] = true;
|
ret[data.replace('header', 'heading')] = true;
|
||||||
@ -680,15 +680,16 @@ function cleanBlock(editor) {
|
|||||||
function drawLink(editor) {
|
function drawLink(editor) {
|
||||||
var cm = editor.codemirror;
|
var cm = editor.codemirror;
|
||||||
var stat = getState(cm);
|
var stat = getState(cm);
|
||||||
|
var type = 'link';
|
||||||
var options = editor.options;
|
var options = editor.options;
|
||||||
var url = 'https://';
|
var url = 'https://';
|
||||||
if (options.promptURLs) {
|
if (!stat[type] && options.promptURLs) {
|
||||||
url = prompt(options.promptTexts.link, 'https://');
|
url = prompt(options.promptTexts.link, 'https://');
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_replaceSelection(cm, stat.link, options.insertTexts.link, url);
|
_toggleLink(cm, type, options.insertTexts.link, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -697,15 +698,16 @@ function drawLink(editor) {
|
|||||||
function drawImage(editor) {
|
function drawImage(editor) {
|
||||||
var cm = editor.codemirror;
|
var cm = editor.codemirror;
|
||||||
var stat = getState(cm);
|
var stat = getState(cm);
|
||||||
|
var type = 'image';
|
||||||
var options = editor.options;
|
var options = editor.options;
|
||||||
var url = 'https://';
|
var url = 'https://';
|
||||||
if (options.promptURLs) {
|
if (!stat[type] && options.promptURLs) {
|
||||||
url = prompt(options.promptTexts.image, 'https://');
|
url = prompt(options.promptTexts.image, 'https://');
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_replaceSelection(cm, stat.image, options.insertTexts.image, url);
|
_toggleLink(cm, type, options.insertTexts.image, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -887,7 +889,41 @@ function _replaceSelection(cm, active, startEnd, url) {
|
|||||||
cm.focus();
|
cm.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _toggleLink(cm, type, startEnd, url)
|
||||||
|
{
|
||||||
|
if (/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
|
||||||
|
return;
|
||||||
|
var stat = getState(cm);
|
||||||
|
|
||||||
|
if (stat[type]) {
|
||||||
|
var startPoint = cm.getCursor('start');
|
||||||
|
var endPoint = cm.getCursor('end');
|
||||||
|
var text = cm.getLine(startPoint.line);
|
||||||
|
var start = text.slice(0, startPoint.ch);
|
||||||
|
var end = text.slice(startPoint.ch);
|
||||||
|
|
||||||
|
var startReg = type == 'link' ? /\[$/ : /!\[$/;
|
||||||
|
start = start.replace(startReg, '');
|
||||||
|
end = end.replace(/\]\(.*?\)/, '');
|
||||||
|
|
||||||
|
cm.replaceRange(start + end, {
|
||||||
|
line: startPoint.line,
|
||||||
|
ch: 0,
|
||||||
|
}, {
|
||||||
|
line: startPoint.line,
|
||||||
|
ch: 99999999999999,
|
||||||
|
});
|
||||||
|
|
||||||
|
startPoint.ch -= startEnd[0].length;
|
||||||
|
if (startPoint !== endPoint) {
|
||||||
|
endPoint.ch -= startEnd[0].length;
|
||||||
|
}
|
||||||
|
cm.setSelection(startPoint, endPoint);
|
||||||
|
cm.focus();
|
||||||
|
} else {
|
||||||
|
_replaceSelection(cm,stat[type],startEnd,url);
|
||||||
|
}
|
||||||
|
}
|
||||||
function _toggleHeading(cm, direction, size) {
|
function _toggleHeading(cm, direction, size) {
|
||||||
if (/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
|
if (/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
|
||||||
return;
|
return;
|
||||||
@ -957,7 +993,6 @@ function _toggleHeading(cm, direction, size) {
|
|||||||
cm.focus();
|
cm.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function _toggleLine(cm, name) {
|
function _toggleLine(cm, name) {
|
||||||
if (/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
|
if (/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
|
||||||
return;
|
return;
|
||||||
@ -1358,7 +1393,7 @@ var toolbarBuiltInButtons = {
|
|||||||
|
|
||||||
var insertTexts = {
|
var insertTexts = {
|
||||||
link: ['[', '](#url#)'],
|
link: ['[', '](#url#)'],
|
||||||
image: [''],
|
image: [''],
|
||||||
table: ['', '\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text | Text | Text |\n\n'],
|
table: ['', '\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text | Text | Text |\n\n'],
|
||||||
horizontalRule: ['', '\n\n-----\n\n'],
|
horizontalRule: ['', '\n\n-----\n\n'],
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user