Skip to content

Commit

Permalink
feat: fix insert error
Browse files Browse the repository at this point in the history
  • Loading branch information
oct16 committed Mar 13, 2020
1 parent cdf359c commit 7aad53d
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 28 deletions.
22 changes: 21 additions & 1 deletion examples/todo.html
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,27 @@ <h1>todos</h1>
app.$mount('.todoapp')

</script>
<script type="module" src="web-replay.js"></script>

<script type="module">
import { record, DB } from './web-replay.js'
DB.then(db => {
db.clear()
const ctr = record({
emitter: data => {
db.add(data)
}
})

const replayButton = document.getElementById('replay')
if (replayButton) {
replayButton.onclick = () => {
ctr.uninstall()
window.open('/replay.html')
}
}
})

</script>
</body>

</html>
1 change: 1 addition & 0 deletions packages/player/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class Container {
const element = parser.parseFromString(HTML, 'text/html').body.firstChild as HTMLElement
element.style.width = this.width + 'px'
element.style.height = this.height + 'px'
element.style.margin = '0 auto'
return (this.container = element)
}

Expand Down
6 changes: 4 additions & 2 deletions packages/player/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@WebReplay/snapshot'
import { Player } from './player'
import { nodeStore } from '@WebReplay/utils'
import { convertVNode } from '@WebReplay/virtual-dom';

export function execFrame(this: Player, snapshot: SnapshotData) {
const { type, data } = snapshot
Expand All @@ -30,7 +31,7 @@ export function execFrame(this: Player, snapshot: SnapshotData) {
const { mutations } = data as DOMObserveData
mutations.forEach((mutate: DOMObserveMutations) => {
const { mType, data } = mutate
const { value, attr, type, parentId, pos, nodeId } = data as ChildListUpdateData &
const { value, attr, type, parentId, pos, nodeId, vNode } = data as ChildListUpdateData &
(CharacterDataUpdateData & AttributesUpdateData)
switch (mType) {
case 'attributes':
Expand All @@ -48,7 +49,7 @@ export function execFrame(this: Player, snapshot: SnapshotData) {
break
case 'childList':
const parentNode = nodeStore.getNode(parentId) as HTMLElement
const targetNode = nodeStore.getNode(nodeId) as Element
const targetNode = nodeStore.getNode(nodeId) as Element || convertVNode(vNode, null)
if (type === ChildListUpdateDataType.DELETE) {
if (targetNode) {
parentNode!.removeChild(targetNode)
Expand All @@ -63,6 +64,7 @@ export function execFrame(this: Player, snapshot: SnapshotData) {
parentNode!.appendChild(textNode)
}
} else {
// it's a ElementNode
parentNode.insertBefore(targetNode, parentNode.childNodes[pos])
}
}
Expand Down
11 changes: 3 additions & 8 deletions packages/player/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { DBPromise, listenerStore, Redux } from '@WebReplay/utils'
import { DBPromise, Redux } from '@WebReplay/utils'
import { Container } from './container'
import { Panel } from './panel'

export async function replay() {
const indexDB = await DBPromise
const { width, height, vNode, data } = await indexDB.getData()

const contain = new Container({
vNode,
width,
height
})

new Panel(contain.container, data)
const box = new Container({ vNode, width, height })
new Panel(box.container, data)

Redux.dispatch({
type: 'PLAY',
Expand Down
4 changes: 0 additions & 4 deletions packages/player/src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,4 @@ export class Player {
this.pause()
this.index = 0
}

setSpeed(speed: number) {
console.log('Set Speed', speed)
}
}
15 changes: 6 additions & 9 deletions packages/snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function mouseObserve(emit: SnapshotEvent<MouseSnapshot>) {
}

function DOMObserve(emit: SnapshotEvent<DOMObserve>) {
const callback: MutationCallback = (records: MutationRecord[]) => {
const mutationCallback: MutationCallback = (records: MutationRecord[]) => {
const mutations: DOMObserveMutations[] = []
function addMutation(mType: 'attributes' | 'characterData' | 'childList') {
return function(data: any) {
Expand Down Expand Up @@ -136,7 +136,6 @@ function DOMObserve(emit: SnapshotEvent<DOMObserve>) {
addedNodes.forEach(node => {
let text
let vNode: any

if (node.nodeType === Node.TEXT_NODE) {
text = node.nodeValue
const pos = Array.from(node.parentNode!.childNodes).indexOf(node as ChildNode)
Expand All @@ -149,12 +148,12 @@ function DOMObserve(emit: SnapshotEvent<DOMObserve>) {
} else {
// reset element for remove reference
vNode = createElement(node as HTMLElement)
convertVNode(vNode, null)
// convertVNode(vNode, null)
const parent = node.parentNode!
joinData({
type: ChildListUpdateDataType.ADD,
parentId: nodeStore.getNodeId(target),
nodeId: vNode && vNode.id,
vNode,
pos:
parent.childNodes.length > 0
? [...parent.childNodes].indexOf(node as ChildNode)
Expand Down Expand Up @@ -189,7 +188,7 @@ function DOMObserve(emit: SnapshotEvent<DOMObserve>) {
}
}

const observer = new MutationObserver(callback)
const observer = new MutationObserver(mutationCallback)
observer.observe(document.body, {
attributeOldValue: true,
attributes: true,
Expand All @@ -213,10 +212,8 @@ function listenInputs(emit: SnapshotEvent<FormElementObserve>) {
const eventTypes = ['input', 'change', 'focus', 'blur']

eventTypes
.map(type => {
return (fn: (e: InputEvent) => void) => {
document.addEventListener(type, fn, { passive: true, capture: true, once: false })
}
.map(type => (fn: (e: InputEvent) => void) => {
document.addEventListener(type, fn, { once: false, passive: true, capture: true })
})
.forEach(handle => handle(handleFn))

Expand Down
5 changes: 3 additions & 2 deletions packages/snapshot/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ export interface CharacterDataUpdateData {
export interface ChildListUpdateData {
type: ChildListUpdateDataType
parentId: number
nodeId?: number
value?: string
nodeId: number
pos: number
value?: string
vNode: VNode
}
export enum ChildListUpdateDataType {
'ADD' = 'ADD',
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {
node(),
sourcemaps(),
html({
template: () => fs.readFileSync('examples/test.html')
template: () => fs.readFileSync('examples/todo.html')
}),
html({
fileName: 'replay.html',
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { string } from 'rollup-plugin-string'
export default {
input: 'index.ts',
output: {
name: 'wr',
format: 'esm',
file: 'dist/web-replay.js',
format: 'umd',
sourcemap: true
},
plugins: [
Expand Down

0 comments on commit 7aad53d

Please sign in to comment.