浏览代码

improve(plugin): types

charlie 4 年之前
父节点
当前提交
f2b2552a6c
共有 4 个文件被更改,包括 25 次插入5 次删除
  1. 2 2
      libs/src/LSPlugin.core.ts
  2. 7 0
      libs/src/LSPlugin.ts
  3. 4 2
      libs/src/LSPlugin.user.ts
  4. 12 1
      libs/src/helpers.ts

+ 2 - 2
libs/src/LSPlugin.core.ts

@@ -22,7 +22,7 @@ import {
   LSPluginPkgConfig,
   StyleOptions,
   StyleString,
-  ThemeOptions,
+  ThemeOptions, UIFrameAttrs,
   UIOptions
 } from './LSPlugin'
 import { snakeCase } from 'snake-case'
@@ -185,7 +185,7 @@ function initMainUIHandlers (pluginLocal: PluginLocal) {
     }
   })
 
-  pluginLocal.on(_('attrs'), (attrs: Record<string, any>) => {
+  pluginLocal.on(_('attrs'), (attrs: Partial<UIFrameAttrs>) => {
     const el = pluginLocal.getMainUI()
     Object.entries(attrs).forEach(([k, v]) => {
       el?.setAttribute(k, v)

+ 7 - 0
libs/src/LSPlugin.ts

@@ -20,6 +20,13 @@ export type StyleOptions = {
   style: StyleString
 }
 
+export type UIFrameAttrs = {
+  draggable: boolean
+  resizable: boolean
+
+  [key: string]: any
+}
+
 export type UIBaseOptions = {
   key?: string
   replace?: boolean

+ 4 - 2
libs/src/LSPlugin.user.ts

@@ -10,7 +10,9 @@ import {
   BlockCommandCallback,
   StyleString,
   ThemeOptions,
-  UIOptions, IHookEvent, BlockIdentity, BlockPageName
+  UIOptions, IHookEvent, BlockIdentity,
+  BlockPageName,
+  UIFrameAttrs
 } from './LSPlugin'
 import Debug from 'debug'
 import * as CSS from 'csstype'
@@ -299,7 +301,7 @@ export class LSPluginUser extends EventEmitter<LSPluginUserEvents> implements IL
     // TODO: update associated baseInfo settings
   }
 
-  setMainUIAttrs (attrs: Record<string, any>): void {
+  setMainUIAttrs (attrs: Partial<UIFrameAttrs>): void {
     this.caller.call('main-ui:attrs', attrs)
   }
 

+ 12 - 1
libs/src/helpers.ts

@@ -39,7 +39,7 @@ export async function getSDKPathRoot (): Promise<string> {
 
   const appPathRoot = await getAppPathRoot()
 
-  return path.join(appPathRoot, 'js')
+  return safetyPathJoin(appPathRoot, 'js')
 }
 
 export function isObject (item: any) {
@@ -110,6 +110,17 @@ export function withFileProtocol (path: string) {
   return path
 }
 
+export function safetyPathJoin (basePath: string, ...parts: Array<string>) {
+  try {
+    const url = new URL(basePath)
+    if (!url.origin) throw new Error(null)
+    const fullPath = path.join(basePath.substr(url.origin.length), ...parts)
+    return url.origin + fullPath
+  } catch (e) {
+    return path.join(basePath, ...parts)
+  }
+}
+
 /**
  * @param timeout milliseconds
  * @param tag string