-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
rrweb: add selection observer #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3fb42fe
cac7d11
1edcc5a
55cafc8
dfbfc6f
9f54a3d
6ec71bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,8 @@ import { | |
| styleDeclarationCallback, | ||
| IWindow, | ||
| MutationBufferParam, | ||
| SelectionRange, | ||
| selectionCallback, | ||
| } from '../types'; | ||
| import MutationBuffer from './mutation'; | ||
|
|
||
|
|
@@ -752,6 +754,47 @@ function initFontObserver({ fontCb, doc }: observerParam): listenerHandler { | |
| }; | ||
| } | ||
|
|
||
| function initSelectionObserver(param: observerParam): listenerHandler { | ||
| const { doc, mirror, blockClass, selectionCb } = param; | ||
| let collapsed = true; | ||
|
|
||
| const updateSelection = () => { | ||
| const selection = doc.getSelection(); | ||
|
|
||
| if (!selection || (collapsed && selection?.isCollapsed)) return; | ||
|
|
||
| collapsed = selection.isCollapsed || false; | ||
|
|
||
| const ranges: SelectionRange[] = []; | ||
| const count = selection.rangeCount || 0; | ||
|
|
||
| for (let i = 0; i < count; i++) { | ||
| const range = selection.getRangeAt(i); | ||
|
|
||
| const { startContainer, startOffset, endContainer, endOffset } = range; | ||
|
|
||
| const blocked = | ||
| isBlocked(startContainer, blockClass, true) || | ||
| isBlocked(endContainer, blockClass, true); | ||
|
|
||
| if (blocked) continue; | ||
|
|
||
| ranges.push({ | ||
| start: mirror.getId(startContainer), | ||
| startOffset, | ||
| end: mirror.getId(endContainer), | ||
| endOffset, | ||
| }); | ||
| } | ||
|
|
||
| selectionCb({ ranges }); | ||
| }; | ||
|
|
||
0jinxing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| updateSelection(); | ||
|
|
||
| return on('selectionchange', updateSelection); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @0jinxing can you confirm that it should be
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @YunFeng0817 I'm not sure how this has escaped everyone's notice if it's been merged! |
||
| } | ||
|
|
||
| function mergeHooks(o: observerParam, hooks: hooksParam) { | ||
| const { | ||
| mutationCb, | ||
|
|
@@ -765,6 +808,7 @@ function mergeHooks(o: observerParam, hooks: hooksParam) { | |
| styleDeclarationCb, | ||
| canvasMutationCb, | ||
| fontCb, | ||
| selectionCb, | ||
| } = o; | ||
| o.mutationCb = (...p: Arguments<mutationCallBack>) => { | ||
| if (hooks.mutation) { | ||
|
|
@@ -832,6 +876,12 @@ function mergeHooks(o: observerParam, hooks: hooksParam) { | |
| } | ||
| fontCb(...p); | ||
| }; | ||
| o.selectionCb = (...p: Arguments<selectionCallback>) => { | ||
| if (hooks.selection) { | ||
| hooks.selection(...p); | ||
| } | ||
| selectionCb(...p); | ||
| }; | ||
| } | ||
|
|
||
| export function initObservers( | ||
|
|
@@ -863,6 +913,8 @@ export function initObservers( | |
| : () => { | ||
| // | ||
| }; | ||
| const selectionObserver = initSelectionObserver(o); | ||
|
|
||
| // plugins | ||
| const pluginHandlers: listenerHandler[] = []; | ||
| for (const plugin of o.plugins) { | ||
|
|
@@ -883,6 +935,7 @@ export function initObservers( | |
| styleSheetObserver(); | ||
| styleDeclarationObserver(); | ||
| fontObserver(); | ||
| selectionObserver(); | ||
| pluginHandlers.forEach((h) => h()); | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import { EventType, eventWithTime, IncrementalSource } from '../../src/types'; | ||
|
|
||
| const now = Date.now(); | ||
|
|
||
| const events: eventWithTime[] = [ | ||
| { | ||
| type: EventType.DomContentLoaded, | ||
| data: {}, | ||
| timestamp: now, | ||
| }, | ||
| { | ||
| type: EventType.Load, | ||
| data: {}, | ||
| timestamp: now + 100, | ||
| }, | ||
| { | ||
| type: EventType.Meta, | ||
| data: { | ||
| href: 'about:blank', | ||
| width: 1920, | ||
| height: 1080, | ||
| }, | ||
| timestamp: now + 200, | ||
| }, | ||
| { | ||
| type: EventType.FullSnapshot, | ||
| data: { | ||
| node: { | ||
| type: 0, | ||
| childNodes: [ | ||
| { | ||
| type: 1, | ||
| name: 'html', | ||
| publicId: '', | ||
| systemId: '', | ||
| id: 2, | ||
| }, | ||
| { | ||
| type: 2, | ||
| tagName: 'html', | ||
| attributes: {}, | ||
| childNodes: [ | ||
| { | ||
| type: 2, | ||
| tagName: 'head', | ||
| attributes: {}, | ||
| childNodes: [ | ||
| { | ||
| type: 3, | ||
| textContent: '\\\\n ', | ||
| id: 5, | ||
| }, | ||
| { | ||
| type: 2, | ||
| tagName: 'meta', | ||
| attributes: { | ||
| charset: 'UTF-8', | ||
| }, | ||
| childNodes: [], | ||
| id: 6, | ||
| }, | ||
| { | ||
| type: 3, | ||
| textContent: '\\\\n ', | ||
| id: 7, | ||
| }, | ||
| ], | ||
| id: 4, | ||
| }, | ||
| { | ||
| type: 3, | ||
| textContent: '\\\\n ', | ||
| id: 8, | ||
| }, | ||
| { | ||
| type: 2, | ||
| tagName: 'body', | ||
| attributes: {}, | ||
| childNodes: [ | ||
| { | ||
| type: 3, | ||
| textContent: '\\\\n Lorem, ipsum\\\\n ', | ||
| id: 10, | ||
| }, | ||
| { | ||
| type: 2, | ||
| tagName: 'span', | ||
| attributes: { | ||
| id: 'startNode', | ||
| }, | ||
| childNodes: [ | ||
| { | ||
| type: 3, | ||
| textContent: | ||
| '\\\\n Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolores culpa\\\\n corporis voluptas odit nobis recusandae inventore, magni praesentium\\\\n maiores perferendis quaerat excepturi officia minus velit voluptate\\\\n placeat minima? Nesciunt, eum!\\\\n ', | ||
| id: 12, | ||
| }, | ||
| ], | ||
| id: 11, | ||
| }, | ||
| { | ||
| type: 3, | ||
| textContent: | ||
| '\\\\n dolor sit amet consectetur adipisicing elit. Ad repellendus quas hic\\\\n deleniti, delectus consequatur voluptas aliquam dolore voluptates repellat\\\\n perferendis aperiam saepe maxime officia rem corporis beatae, assumenda\\\\n doloribus.\\\\n ', | ||
| id: 13, | ||
| }, | ||
| { | ||
| type: 2, | ||
| tagName: 'span', | ||
| attributes: { | ||
| id: 'endNode', | ||
| }, | ||
| childNodes: [ | ||
| { | ||
| type: 3, | ||
| textContent: | ||
| '\\\\n Lorem ipsum dolor sit amet consectetur adipisicing elit. Recusandae\\\\n explicabo omnis dolores magni, ea doloribus possimus debitis reiciendis\\\\n distinctio perferendis nihil ipsum officiis pariatur laboriosam quas,\\\\n corrupti vero vitae minus.\\\\n ', | ||
| id: 15, | ||
| }, | ||
| ], | ||
| id: 14, | ||
| }, | ||
| { | ||
| type: 3, | ||
| textContent: '\\\\n \\\\n ', | ||
| id: 16, | ||
| }, | ||
| { | ||
| type: 2, | ||
| tagName: 'script', | ||
| attributes: {}, | ||
| childNodes: [ | ||
| { | ||
| type: 3, | ||
| textContent: 'SCRIPT_PLACEHOLDER', | ||
| id: 18, | ||
| }, | ||
| ], | ||
| id: 17, | ||
| }, | ||
| { | ||
| type: 3, | ||
| textContent: '\\\\n \\\\n \\\\n\\\\n', | ||
| id: 19, | ||
| }, | ||
| ], | ||
| id: 9, | ||
| }, | ||
| ], | ||
| id: 3, | ||
| }, | ||
| ], | ||
| id: 1, | ||
| }, | ||
| initialOffset: { | ||
| left: 0, | ||
| top: 0, | ||
| }, | ||
| }, | ||
| timestamp: now + 300, | ||
| }, | ||
| { | ||
| type: EventType.IncrementalSnapshot, | ||
| data: { | ||
| source: IncrementalSource.Selection, | ||
| ranges: [ | ||
| { | ||
| start: 12, | ||
| startOffset: 11, | ||
| end: 15, | ||
| endOffset: 6, | ||
| }, | ||
| ], | ||
| }, | ||
| timestamp: now + 400, | ||
| }, | ||
| ]; | ||
|
|
||
| export default events; |
Uh oh!
There was an error while loading. Please reload this page.