From aaf1ba2bb628dfe5f392881c01522369dc85a2e1 Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Sun, 12 Apr 2020 10:21:41 +0200 Subject: [PATCH] Additions to selector to support image fragments --- src/selection/Selection.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/selection/Selection.js b/src/selection/Selection.js index 5264d06..c8e5416 100644 --- a/src/selection/Selection.js +++ b/src/selection/Selection.js @@ -20,8 +20,13 @@ export default class Selection { /** Creates a copy of this selection **/ clone = opt_props => { - const selectors = this._stub.target.selector.map(s => ({ ...s })); // Clone selectors - const cloned = new Selection(selectors); + // Clone selector(s) + const { selector } = this._stub.target; + + const clonedSelector = Array.isArray(selector) ? + selector.map(s => ({ ...s })) : { ...selector }; + + const cloned = new Selection(clonedSelector); if (opt_props) cloned._stub = { ...cloned._stub, ...opt_props }; @@ -47,7 +52,15 @@ export default class Selection { } selector = type => { - return this._stub.target.selector.find(t => t.type === type); + const { target } = this._stub; + + if (target.selector) { + // Normalize to array + const selectors = Array.isArray(target.selector) ? + target.selector : [ target.selector ]; + + return selectors.find(s => s.type === type); + } } /** Shorthand for the 'exact' field of the TextQuoteSelector **/