Skip to content

Commit

Permalink
feat: be able to unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest committed Sep 11, 2022
1 parent 1af3d85 commit 0d5244b
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 69 deletions.
2 changes: 1 addition & 1 deletion cypress/integration/array/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="cypress" />

import path from 'path'
import { mount } from '../../../dist/object-visualizer.esm'
import { mount } from '../../utils/index'
import {
primitiveInArray,
arrayInArray,
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/object/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="cypress" />

import path from 'path'
import { mount } from '../../../dist/object-visualizer.esm'
import { mount } from '../../utils/index'
import {
primitiveInObject,
arrayInObject,
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/primitive/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="cypress" />

import path from 'path'
import { mount } from '../../../dist/object-visualizer.esm'
import { mount } from '../../utils/index'

const options = {
rootName: '$',
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions cypress/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createApp } from 'vue'
import { ObjectVisualizer } from '../../dist/object-visualizer.js'

export const mount = (data, el, options) => {
const app = createApp(ObjectVisualizer, {
data,
...options,
})
app.mount(el)
}
36 changes: 36 additions & 0 deletions lib/ObjectVisualizer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script setup lang="ts">
import { getCurrentInstance } from 'vue'
import Wrapper from './components/Wrapper.vue'
withDefaults(
defineProps<{
data: any
rootName?: string
expandOnCreatedAndUpdated?: (path: string[]) => boolean
getKeys?: (object: Record<string, any>, path: string[]) => string[]
uid?: number
}>(),
{
rootName: '',
expandOnCreatedAndUpdated: () => false,
getKeys: (object: Record<string, any>, path: string[]) =>
Object.keys(object),
uid: getCurrentInstance()?.uid,
},
)
</script>

<template>
<section class="object-visualizer">
<Wrapper
:data="data"
:name="rootName"
:expand-on-created-and-updated="expandOnCreatedAndUpdated"
:get-keys="getKeys"
:object-visualizer-uid="0"
:path="[]"
:aria-level="0"
>
</Wrapper>
</section>
</template>
6 changes: 5 additions & 1 deletion lib/components/BooleanWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<span class="boolean" role="treeitem" :aria-level="ariaLevel" :id="id">
<span class="boolean" :role="role" :aria-level="ariaLevel" :id="id">
<span class="key">{{ name }}</span>
<span v-if="name !== ''" class="separator">:&nbsp;</span>
<span class="value">{{ data }}</span>
Expand All @@ -25,6 +25,10 @@ export default defineComponent({
required: true,
type: String,
},
role: {
required: true,
type: String,
},
ariaLevel: {
required: true,
type: Number,
Expand Down
6 changes: 5 additions & 1 deletion lib/components/NullWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<span class="null" role="treeitem" :aria-level="ariaLevel" :id="id">
<span class="null" :role="role" :aria-level="ariaLevel" :id="id">
<span class="key">{{ name }}</span>
<span v-if="name !== ''" class="separator">:&nbsp;</span>
<span class="value">null</span>
Expand All @@ -25,6 +25,10 @@ export default defineComponent({
required: true,
type: String,
},
role: {
required: true,
type: String,
},
ariaLevel: {
required: true,
type: Number,
Expand Down
6 changes: 5 additions & 1 deletion lib/components/NumberWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<span class="number" role="treeitem" :aria-level="ariaLevel" :id="id">
<span class="number" :role="role" :aria-level="ariaLevel" :id="id">
<span class="key">{{ name }}</span>
<span v-if="name !== ''" class="separator">:&nbsp;</span>
<span class="value">{{ data }}</span>
Expand All @@ -25,6 +25,10 @@ export default defineComponent({
required: true,
type: String,
},
role: {
required: true,
type: String,
},
ariaLevel: {
required: true,
type: Number,
Expand Down
2 changes: 1 addition & 1 deletion lib/components/ObjectWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default defineComponent({
return (
objectToString(path) === 'Array' &&
(path as unknown[]).every(
(key) =>
(key: unknown) =>
objectToString(key) === 'String' ||
objectToString(key) === 'Number',
)
Expand Down
6 changes: 5 additions & 1 deletion lib/components/StringWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<span class="string" role="treeitem" :aria-level="ariaLevel" :id="id">
<span class="string" :role="role" :aria-level="ariaLevel" :id="id">
<span class="key">{{ name }}</span>
<span v-if="name !== ''" class="separator">:&nbsp;</span>
<span class="quotes">"</span>
Expand Down Expand Up @@ -27,6 +27,10 @@ export default defineComponent({
required: true,
type: String,
},
role: {
required: true,
type: String,
},
ariaLevel: {
required: true,
type: Number,
Expand Down
38 changes: 25 additions & 13 deletions lib/components/Wrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
path.length === 0 ? 'root' : path.join('-')
}`"
:role="role"
:aria-level="ariaLevel + 1"
:aria-level="role ? ariaLevel + 1 : void 0"
:class="ariaLevel === 0 ? 'object-visualizer' : void 0"
/>
</template>

Expand All @@ -25,7 +26,7 @@ import StringWrapper from './StringWrapper.vue'
import ArrayWrapper from './ArrayWrapper.vue'
import ObjectWrapper from './ObjectWrapper.vue'
import { objectToString } from '../util'
import { computed, defineComponent } from 'vue'
import { computed, defineComponent, PropType } from 'vue'
const TYPE_TO_COMPONENT = {
Null: 'null-wrapper',
Expand All @@ -40,13 +41,13 @@ const Wrapper = defineComponent({
inheritAttrs: false,
props: {
path: {
type: Array,
required: true,
type: Array as PropType<string[]>,
validator(path: unknown) {
return (
objectToString(path) === 'Array' &&
(path as unknown[]).every(
(key) =>
(key: unknown) =>
objectToString(key) === 'String' ||
objectToString(key) === 'Number',
)
Expand Down Expand Up @@ -91,24 +92,35 @@ const Wrapper = defineComponent({
required: true,
type: Number,
},
role: {
default: 'group',
type: String,
},
ariaLevel: {
required: true,
type: Number,
},
},
setup(props) {
const is = computed(
() =>
TYPE_TO_COMPONENT[
objectToString(props.data) as keyof typeof TYPE_TO_COMPONENT
],
const type = computed(
() => objectToString(props.data) as keyof typeof TYPE_TO_COMPONENT,
)
const is = computed(() => TYPE_TO_COMPONENT[type.value])
const role = computed(() => {
if (props.ariaLevel === 0) {
if (type.value === 'Array' || type.value === 'Object') {
return 'tree'
} else {
return void 0
}
} else {
if (type.value === 'Array' || type.value === 'Object') {
return 'group'
} else {
return 'treeitem'
}
}
})
return {
is,
role,
objectToString,
TYPE_TO_COMPONENT,
}
Expand Down
5 changes: 0 additions & 5 deletions lib/config.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions lib/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as ObjectVisualizer } from './ObjectVisualizer.vue'
31 changes: 0 additions & 31 deletions lib/mount.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/node": "^18.7.16",
"@vitejs/plugin-vue": "^3.1.0",
"csso": "^5.0.5",
"cypress": "^10.7.0",
"cypress": "^9",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"npm-run-all": "^4.1.5",
Expand Down
14 changes: 5 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { minify } from 'csso'
export default defineConfig({
build: {
lib: {
entry: path.resolve(__dirname, 'lib/index.ts'),
entry: path.resolve(__dirname, 'lib/main.ts'),
name: 'ObjectVisualizer',
fileName: 'object-visualizer',
},
Expand Down

0 comments on commit 0d5244b

Please sign in to comment.