Skip to content

Commit

Permalink
+2
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Jan 4, 2025
1 parent 1fb0405 commit 037c7c3
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions dom/range/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ namespace $ {
}

static from_selection( sel = $mol_dom_context.getSelection()! ) {
return this.from_native( sel?.getRangeAt(0) )
return new this(
new $mol_dom_point( sel.anchorNode!, sel.anchorOffset ),
new $mol_dom_point( sel.focusNode!, sel.focusOffset ),
)
}

static from_native( range: Range ) {
Expand All @@ -37,6 +40,10 @@ namespace $ {
return this.anchor.node === this.extend.node && this.anchor.pos === this.extend.pos
}

swap() {
return new $mol_dom_range( this.extend, this.anchor )
}

expand() {
return new $mol_dom_range(
this.anchor.jump(-1)!,
Expand Down Expand Up @@ -73,21 +80,26 @@ namespace $ {
}

select() {

const [ anchorNode, anchorOffset ] = this.anchor.native()
const [ focusNode, focusOffset ] = this.extend.native()

const sel = $mol_dom_context.document.getSelection()!
sel.removeAllRanges()
sel.addRange( this.native() )
sel.setBaseAndExtent( anchorNode, anchorOffset, focusNode, focusOffset )

return this
}

native() {

const range = $mol_dom_context.document.createRange()

if( this.extend.is_tail() ) range.setEndAfter( this.extend.node )
else range.setEnd( this.extend.node, this.extend.pos )

if( this.anchor.is_tail() ) range.setStartAfter( this.anchor.node )
else range.setStart( this.anchor.node, this.anchor.pos )

if( this.extend.is_tail() ) range.setEndAfter( this.extend.node )
else range.setEnd( this.extend.node, this.extend.pos )

return range
}

Expand Down

0 comments on commit 037c7c3

Please sign in to comment.