Created
June 16, 2018 05:22
-
-
Save nathanwoulfe/3f685e3c1f26a6eda3d3d7911075ca47 to your computer and use it in GitHub Desktop.
Restore anchors in TinyMce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* this has been heavily edited for brevity */ | |
tinymce.PluginManager.add('link', function (editor) { | |
/* ... */ | |
function showDialog(linkList) { | |
/* ... */ | |
var selectedElm = selection.getNode(); | |
var anchorElm = dom.getParent(selectedElm, 'a[href]'); | |
data.text = initialText = anchorElm ? (anchorElm.innerText || anchorElm.textContent) : selection.getContent({ format: 'text' }); | |
data.href = anchorElm ? dom.getAttrib(anchorElm, 'href') : ''; | |
data.rel = anchorElm ? dom.getAttrib(anchorElm, 'rel') : ''; | |
// if the node has been edited previously and an anchor value set, it will exist as a data attribute - fetch it here | |
data.target = anchorElm ? dom.getAttrib(anchorElm, 'target') : ''; | |
/* ... */ | |
//if we already have a link selected, we want to pass that data over to the dialog | |
if (anchorElm) { | |
// Ndon't need to re-fetch the anchor url, it's already available as data.href (ln 14). Probably worth cleaning up | |
var $anchor = $(anchorElm), | |
anchorText = '', | |
url = $anchor.attr('href').split(/(\?|#)/); | |
// if the split url is [3], 1 = '#' or '?', 2 is the value, so stick em together | |
if (url.length === 3) { | |
anchorText = url[1] + url[2]; | |
} | |
// updates the currentTarget variable to include the anchor value | |
currentTarget = { | |
name: $anchor.attr("title"), | |
url: url[0], | |
target: $anchor.attr("target"), | |
anchor: anchorText | |
}; | |
//locallink detection, we do this here, to avoid poluting the dialogservice | |
//so the dialog service can just expect to get a node-like structure | |
if (currentTarget.url.indexOf("localLink:") > 0) { | |
currentTarget.id = currentTarget.url.substring(currentTarget.url.indexOf(":") + 1, currentTarget.url.length - 1); | |
} | |
} | |
dialogService.linkPicker({ | |
currentTarget: currentTarget, | |
callback: function (data) { | |
if (data) { | |
var href = data.url; | |
// allow anchors to be set in the popup | |
if (data.anchor) { | |
// check for ? and #, if neither are in the string, set the prefix based on the presence of '=' | |
if (data.anchor.indexOf('?') === -1 && data.anchor.indexOf('#') === -1) { | |
data.anchor = (data.anchor.indexOf('=') === -1 ? '#' : '?') + data.anchor; | |
} | |
} | |
// finally, add the anchor value back into the dom | |
function insertLink() { | |
if (anchorElm) { | |
dom.setAttribs(anchorElm, { | |
href: href, | |
title: data.name, | |
target: data.target ? data.target : null, | |
rel: data.rel ? data.rel : null, | |
'data-id': data.id ? data.id : null, | |
'data-anchor': data.anchor ? data.anchor : '' | |
}); | |
selection.select(anchorElm); | |
editor.execCommand('mceEndTyping'); | |
} else { | |
editor.execCommand('mceInsertLink', false, { | |
href: href, | |
title: data.name, | |
target: data.target ? data.target : null, | |
rel: data.rel ? data.rel : null, | |
'data-id': data.id ? data.id : null, | |
'data-anchor': data.anchor ? data.anchor : '' | |
}); | |
} | |
} | |
if (!href) { | |
editor.execCommand('unlink'); | |
return; | |
} | |
//if we have an id, it must be a locallink:id, aslong as the isMedia flag is not set | |
if (data.id && (angular.isUndefined(data.isMedia) || !data.isMedia)) { | |
href = "/{localLink:" + data.id + "}" + (data.anchor ? data.anchor : ''); | |
insertLink(); | |
return; | |
} | |
// Is email and not //[email protected] | |
if (href.indexOf('@') > 0 && href.indexOf('//') == -1 && href.indexOf('mailto:') == -1) { | |
href = 'mailto:' + href; | |
insertLink(); | |
return; | |
} | |
// Is www. prefixed | |
if (/^\s*www\./i.test(href)) { | |
href = 'http://' + href + (data.anchor ? data.anchor : ''); | |
insertLink(); | |
return; | |
} | |
insertLink(); | |
} | |
} | |
}); | |
} | |
/* ... */ | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment