active-editor-line.ts 792 B

1234567891011121314151617181920212223242526
  1. export const activeEditorLine = () => {
  2. // wait for the selection to be in the editor content DOM
  3. cy.window().then(win => {
  4. cy.get('.cm-content').should($el => {
  5. const contentNode = $el.get(0)
  6. const range = win.getSelection()?.getRangeAt(0)
  7. expect(range?.intersectsNode(contentNode)).to.be.true
  8. })
  9. })
  10. // find the closest line block ancestor of the selection
  11. return cy.window().then(win => {
  12. const activeNode = win.getSelection()?.focusNode
  13. if (!activeNode) {
  14. return cy.wrap(null)
  15. }
  16. // use the parent element if this is a node, e.g. text
  17. const activeElement = (
  18. 'closest' in activeNode ? activeNode : activeNode.parentElement
  19. ) as HTMLElement | undefined
  20. return cy.wrap(activeElement?.closest('.cm-line'))
  21. })
  22. }