Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Fix Scribe adding links to other contenteditables #522

Merged
merged 1 commit into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/api/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,35 +101,36 @@ define(function () {
nodeHelpers.getAncestor(node, scribe.el, nodeFilter);
};

Selection.prototype.placeMarkers = function () {
Selection.prototype.isInScribe = function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff doesn't really show this nicely but placeMarkers still exists but it has functionality to check whether the selection was in Scribe that I broke out into a method that could be used in create-link.

var range = this.range;
if (!range) {
return;
}
return range
//we need to ensure that the scribe's element lives within the current document to avoid errors with the range comparison (see below)
//one way to do this is to check if it's visible (is this the best way?).
&& document.contains(scribe.el)
//we want to ensure that the current selection is within the current scribe node
//if this isn't true scribe will place markers within the selections parent
//we want to ensure that scribe ONLY places markers within it's own element
&& scribe.el.contains(range.startContainer)
&& scribe.el.contains(range.endContainer);
}

//we need to ensure that the scribe's element lives within the current document to avoid errors with the range comparison (see below)
//one way to do this is to check if it's visible (is this the best way?).
if (!document.contains(scribe.el)) {
Selection.prototype.placeMarkers = function () {
if (!this.isInScribe()) {
return;
}

//we want to ensure that the current selection is within the current scribe node
//if this isn't true scribe will place markers within the selections parent
//we want to ensure that scribe ONLY places markers within it's own element
if (scribe.el.contains(range.startContainer) && scribe.el.contains(range.endContainer)) {
// insert start marker
insertMarker(range.cloneRange(), createMarker());

if (! range.collapsed ) {
// End marker
var rangeEnd = range.cloneRange();
rangeEnd.collapse(false);
insertMarker(rangeEnd, createMarker());
}
// insert start marker
insertMarker(range.cloneRange(), createMarker());

this.selection.removeAllRanges();
this.selection.addRange(range);
if (! range.collapsed ) {
// End marker
var rangeEnd = range.cloneRange();
rangeEnd.collapse(false);
insertMarker(rangeEnd, createMarker());
}

this.selection.removeAllRanges();
this.selection.addRange(range);
};

Selection.prototype.getMarkers = function () {
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/core/patches/commands/create-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ define(function () {
createLinkCommand.execute = function (value) {
var selection = new scribe.api.Selection();

/**
* make sure we're not touching any none Scribe elements
* in the page
*/
if (!scribe.api.Selection().isInScribe()) {
return;
}

/**
* Firefox does not create a link when selection is collapsed
* so we create it manually. http://jsbin.com/tutufi/2/edit?js,output
Expand Down