Add limitTo

This commit is contained in:
Chi Vinh Le
2018-03-25 01:32:21 +01:00
parent 2757791672
commit 8586da97fb
2 changed files with 13 additions and 13 deletions
@@ -1,22 +1,17 @@
import { selectionIsInside } from './dom';
/**
* An instance of API is passed to all the buttons to
* interact with RTE, which servers as a clean abstraction.
*/
export default class API {
constructor(contentEditable, onChange, canUndo, canRedo, undo, redo) {
this.contentEditable = contentEditable;
constructor(container, onChange, canUndo, canRedo, undo, redo) {
this.container = container;
this.broadcastChange = onChange;
this.canUndo = canUndo;
this.canRedo = canRedo;
this.undo = undo;
this.redo = redo;
}
isSelectionInside() {
return selectionIsInside(this.contentEditable);
}
focus() {
this.contentEditable.focus();
this.container.focus();
}
}
@@ -1,7 +1,8 @@
/**
* Find ancestor with given tag or whith callback returning true.
* If `limitTo` is passed, the search is limited to this container.
*/
export function findAncestor(node, tagOrCallback) {
export function findAncestor(node, tagOrCallback, limitTo) {
const callback =
typeof tagOrCallback === 'function'
? tagOrCallback
@@ -11,6 +12,9 @@ export function findAncestor(node, tagOrCallback) {
if (callback(node)) {
return node;
}
if (limitTo && node.isSameNode(limitTo)) {
return null;
}
}
return null;
}
@@ -38,9 +42,10 @@ export function findChild(node, tagOrCallback) {
/**
* Find an node intersecting with the selection with given tag or
* with callback returning true.
* with callback returning true. If `limitTo` is passed, the search
* is limited to this container.
*/
export function findIntersecting(tagOrCallback) {
export function findIntersecting(tagOrCallback, limitTo) {
const callback =
typeof tagOrCallback === 'function'
? tagOrCallback
@@ -55,7 +60,7 @@ export function findIntersecting(tagOrCallback) {
return range.startContainer;
}
const ancestor = findAncestor(range.startContainer, callback);
const ancestor = findAncestor(range.startContainer, callback, limitTo);
if (ancestor) {
return ancestor;
}
@@ -226,7 +231,7 @@ function ensureEndMarker(node) {
* Returns true if selection is completely inside
* given node.
*/
export function selectionIsInside(node) {
export function isSelectionInside(node) {
const range = getSelectionRange();
return (
range &&