diff --git a/dom/point/point.ts b/dom/point/point.ts index 4935c0fd07..8b46caf2c4 100644 --- a/dom/point/point.ts +++ b/dom/point/point.ts @@ -70,6 +70,14 @@ namespace $ { tail() { return $mol_dom_point.tail( this.node ) } + + native() { + if( this.is_tail() ) return [ + this.node.parentNode!, + [ ... this.node.parentNode!.childNodes ].indexOf( this.node as ChildNode ), + ] as const + return [ this.node, this.pos ] as const + } /** Point near the node. -1: before, +1: after. */ static near( node: Node, axis: -1 | 1 ) { diff --git a/dom/range/range.ts b/dom/range/range.ts index 311a40ff27..ae126c75f7 100644 --- a/dom/range/range.ts +++ b/dom/range/range.ts @@ -53,17 +53,25 @@ namespace $ { } point_compare( point: $mol_dom_point ) { - if( point.is_tail() ) return this.native().comparePoint( - point.node.parentNode!, - [ ... point.node.parentNode!.childNodes ].indexOf( point.node as ChildNode ), - ) - else return this.native().comparePoint( point.node, point.pos ) + return this.native().comparePoint( ... point.native() ) + } + + point_bound( point: $mol_dom_point ) { + const zone = this.point_compare( point ) + return zone < 0 ? this.anchor : zone > 0 ? this.extend : point } range_contains( range: $mol_dom_range ) { return ( this.point_compare( range.anchor ) === 0 )&&( this.point_compare( range.extend ) === 0 ) } + range_bounds( range: $mol_dom_range ) { + return new $mol_dom_range( + this.point_bound( range.anchor ), + this.point_bound( range.extend ), + ) + } + select() { const sel = $mol_dom_context.document.getSelection()! sel.removeAllRanges()