Skip to content

Commit

Permalink
feat(runtime): allow rendering to NSVRoot containers
Browse files Browse the repository at this point in the history
  • Loading branch information
rigor789 committed Apr 25, 2020
1 parent cab807f commit 57ebd28
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 6 additions & 3 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Application } from '@nativescript/core'
import { debug } from '@nativescript-vue/shared'
import { nodeOps } from './nodeOps'
import { patchProp } from './patchProp'
import { INSVElement } from './nodes'
import { INSVElement, NSVRoot } from './nodes'
import './registry'

const rendererOptions = {
Expand Down Expand Up @@ -41,9 +41,12 @@ function runApp(root: ComponentPublicInstance): ComponentPublicInstance {
return root
}

export const render = ((vnode: VNode | null, container: INSVElement) => {
export const render = ((
vnode: VNode | null,
container: INSVElement | NSVRoot
) => {
ensureRenderer().render(vnode, container)
}) as RootRenderFunction<INSVElement>
}) as RootRenderFunction<INSVElement | NSVRoot>

export const createApp = ((...args) => {
const app = ensureRenderer().createApp(...args)
Expand Down
24 changes: 15 additions & 9 deletions packages/runtime/src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
getViewClass,
getViewMeta,
normalizeElementName,
NSVViewMeta
NSVViewMeta,
} from './registry'
import { ELEMENT_REF } from '@nativescript-vue/runtime'
import { debug } from '@nativescript-vue/shared'
Expand All @@ -17,7 +17,7 @@ export const enum NSVNodeTypes {
TEXT = 'text',
ELEMENT = 'element',
COMMENT = 'comment',
ROOT = 'root'
ROOT = 'root',
}

// View Flags indicate the kind of view the element is
Expand All @@ -29,7 +29,7 @@ export const enum NSVViewFlags {
SKIP_ADD_TO_DOM = 1 << 0,
CONTENT_VIEW = 1 << 1,
LAYOUT_VIEW = 1 << 2,
NO_CHILDREN = 1 << 3
NO_CHILDREN = 1 << 3,
}

export interface INSVNode {
Expand Down Expand Up @@ -207,7 +207,7 @@ export class NSVElement extends NSVNode implements INSVElement {
}

const refIndex = this.childNodes.findIndex(
node => node.nodeId === anchor.nodeId
(node) => node.nodeId === anchor.nodeId
)

if (refIndex === -1) {
Expand All @@ -226,8 +226,8 @@ export class NSVElement extends NSVNode implements INSVElement {
// todo: potentially refactor based on my benchmark:
// https://www.measurethat.net/Benchmarks/Show/7450/0/filter-findindex
const trueIndex = this.childNodes
.filter(node => node.nodeType === NSVNodeTypes.ELEMENT)
.findIndex(node => node.nodeId === el.nodeId)
.filter((node) => node.nodeType === NSVNodeTypes.ELEMENT)
.findIndex((node) => node.nodeId === el.nodeId)

this.addChild(el, trueIndex)
}
Expand All @@ -240,7 +240,7 @@ export class NSVElement extends NSVNode implements INSVElement {
}

removeChild(el: INSVNode) {
const index = this.childNodes.findIndex(node => node.nodeId === el.nodeId)
const index = this.childNodes.findIndex((node) => node.nodeId === el.nodeId)

if (index > -1) {
this.childNodes.splice(index, 1)
Expand All @@ -266,7 +266,7 @@ export class NSVElement extends NSVNode implements INSVElement {
this.setAttribute(
'text',
this.childNodes
.filter(node => node.nodeType === NSVNodeTypes.TEXT)
.filter((node) => node.nodeType === NSVNodeTypes.TEXT)
.reduce((text: string, currentNode) => {
return text + currentNode.text
}, '')
Expand All @@ -291,11 +291,17 @@ export class NSVText extends NSVNode {
}

export class NSVRoot extends NSVNode {
el?: NSVElement

constructor() {
super(NSVNodeTypes.ROOT)
}

appendChild() {
appendChild(el: INSVNode) {
// console.log(`NSVRoot->appendChild(${el.nodeType})`)
if (el instanceof NSVElement) {
this.el = el
}
// no-op
}
}
Expand Down

0 comments on commit 57ebd28

Please sign in to comment.