import { VNode , VNodeData } from "../vnode" ; export interface AttachData { [ key ] : any ; [ i ] : any ; placeholder ? : any ; real ? : Node ; } interface VNodeDataWithAttach extends VNodeData { attachData : AttachData ; } interface VNodeWithAttachData extends VNode { data : VNodeDataWithAttach ; } function pre ( vnode , newVnode ) { const attachData = vnode . data . attachData ; newVnode . data . attachData . placeholder = attachData . placeholder ; newVnode . data . attachData . real = attachData . real ; vnode . elm = vnode . data . attachData . real ; } function post ( _ , vnode ) { vnode . elm = vnode . data . attachData . placeholder ; } function destroy ( vnode ) { if ( vnode . elm !== undefined ) { ( vnode . elm . parentNode as HTMLElement ) . removeChild ( vnode . elm ) ; } vnode . elm = vnode . data . attachData . real ; } function create ( _ , vnode ) { const real = vnode . elm ; const attachData = vnode . data . attachData ; const placeholder = document . createElement ( "span" ) ; vnode . elm = placeholder ; attachData . target . appendChild ( real ) ; attachData . real = real ; attachData . placeholder = placeholder ; } export function attachTo ( target , vnode ) { if ( vnode . data === undefined ) vnode . data = { } ; if ( vnode . data . hook === undefined ) vnode . data . hook = { } ; const data = vnode . data ; const hook = vnode . data . hook ; data . attachData = { target : target , placeholder : undefined , real : undefined } ; hook . create = create ; hook . prepatch = pre ; hook . postpatch = post ; hook . destroy = destroy ; return vnode ; }