API change: selections now take target as arg, not selectors

This commit is contained in:
Rainer Simon 2020-05-09 16:12:37 +02:00
parent 9f11f88fa6
commit 1508ed1207
2 changed files with 15 additions and 19 deletions

View File

@ -8,25 +8,19 @@ import uuid from 'uuid/v1';
*/
export default class Selection {
constructor(selectors) {
constructor(target) {
this._stub = {
type: 'Selection',
body: [],
target: {
selector: selectors
}
target
}
}
/** Creates a copy of this selection **/
clone = opt_props => {
// Clone selector(s)
const { selector } = this._stub.target;
const clonedSelector = Array.isArray(selector) ?
selector.map(s => ({ ...s })) : { ...selector };
const cloned = new Selection(clonedSelector);
// Deep-clone target
const clonedTarget = JSON.parse(JSON.stringify(this._stub.target));
const cloned = new Selection(clonedTarget);
if (opt_props)
cloned._stub = { ...cloned._stub, ...opt_props };

View File

@ -37,14 +37,16 @@ export const rangeToSelection = (range, containerEl) => {
const quote = range.toString();
const start = rangeBefore.toString().length;
return new Selection([{
return new Selection({
selector: [{
type: 'TextQuoteSelector',
exact: quote
}, {
type: 'TextPositionSelector',
start: start,
end: start + quote.length
}]);
}]
});
};