|
@@ -1,33 +1,58 @@
|
|
|
import cover from './cover'
|
|
|
-import { setup } from './view'
|
|
|
+import { setup } from './association'
|
|
|
import { loadLib } from '@/utils'
|
|
|
|
|
|
-import type { ModelType } from '@/api'
|
|
|
+import type { ModelAttrs, Model } from '@/api'
|
|
|
import type { Emitter } from 'mitt'
|
|
|
|
|
|
-type ToChangeAPI<T extends Record<string, any>> = {
|
|
|
- [key in keyof T as `change${Capitalize<key & string>}`]: T[key]
|
|
|
-}
|
|
|
|
|
|
+type SceneModelAttrs = ModelAttrs & { select: boolean }
|
|
|
+export type SceneModel = Emitter<Pick<SceneModelAttrs, 'position' | 'rotation' | 'select'>>
|
|
|
+ & ToChangeAPI<Omit<SceneModelAttrs, 'position' | 'rotation'>>
|
|
|
+ & {
|
|
|
+ destroy: () => void
|
|
|
+ enterRotateMode: () => void
|
|
|
+ leaveRotateMode: () => void
|
|
|
+ enterMoveMode: () => void
|
|
|
+ leaveMoveMode: () => void
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+export type AddModelProps = Pick<Model, 'type' | 'url'> & ModelAttrs
|
|
|
export interface SDK {
|
|
|
- addModel: (props: AddModelProps) => Model
|
|
|
+ layout: HTMLDivElement,
|
|
|
+ addModel: (props: AddModelProps) => SceneModel
|
|
|
}
|
|
|
+
|
|
|
|
|
|
-export type ModelEventMap = {
|
|
|
- select: boolean,
|
|
|
- scale: number,
|
|
|
- opacity: number,
|
|
|
- bottom: number
|
|
|
-}
|
|
|
-export type AddModelProps = {
|
|
|
- url: string,
|
|
|
- type: ModelType
|
|
|
+const presetViewElement = (layout: HTMLDivElement) => {
|
|
|
+ const style = getComputedStyle(layout)
|
|
|
+ const allows = ['relative', 'absolute', 'fixed']
|
|
|
+
|
|
|
+ if (!allows.includes(style.position)) {
|
|
|
+ layout.style.position = 'relative'
|
|
|
+ }
|
|
|
+ const el = document.createElement('div')
|
|
|
+ el.style.cssText = `
|
|
|
+ position: absolute;
|
|
|
+ left: 0;
|
|
|
+ right: 0;
|
|
|
+ top: 0;
|
|
|
+ bottom: 0;
|
|
|
+ pointer-events: none;
|
|
|
+ z-index:101
|
|
|
+ `
|
|
|
+ layout.appendChild(el)
|
|
|
+ return el
|
|
|
}
|
|
|
-export type Model = Emitter<ModelEventMap> & ToChangeAPI<ModelEventMap>
|
|
|
+
|
|
|
|
|
|
export let sdk: SDK
|
|
|
-export type InialSDKProps = { canvas: HTMLCanvasElement }
|
|
|
-export const initialSDK = async (el: InialSDKProps) => {
|
|
|
+export type InialSDKProps = { layout: HTMLDivElement }
|
|
|
+let initialed = false
|
|
|
+export const initialSDK = async (props: InialSDKProps) => {
|
|
|
+ if (initialed) return;
|
|
|
+ initialed = true
|
|
|
const libs = [
|
|
|
`/lib/proj4/proj4.js`,
|
|
|
`/lib/jquery/jquery-3.1.1.min.js`,
|
|
@@ -36,10 +61,11 @@ export const initialSDK = async (el: InialSDKProps) => {
|
|
|
]
|
|
|
await Promise.all(libs.map(loadLib))
|
|
|
await loadLib(`/lib/potree/potree.js`)
|
|
|
- sdk = cover(el) as SDK
|
|
|
- setup(sdk)
|
|
|
+
|
|
|
+ sdk = cover(props.layout) as SDK
|
|
|
+ setup(sdk, presetViewElement(props.layout))
|
|
|
}
|
|
|
|
|
|
|
|
|
-export * from './view'
|
|
|
+export * from './association'
|
|
|
export default sdk
|