|
|
@ -1,7 +1,7 @@ |
|
|
|
import type { ComputedRef, InjectionKey, UnwrapNestedRefs } from 'vue' |
|
|
|
import type { WritableComputedRef, InjectionKey, UnwrapNestedRefs } from 'vue' |
|
|
|
import { computed, inject, provide } from 'vue' |
|
|
|
import { computed, inject, isReactive, isRef, provide } from 'vue' |
|
|
|
import type { Block, Category, Module, Page } from './types' |
|
|
|
import type { Block, Category, Module, Page } from './types' |
|
|
|
import { valueOf } from './utils' |
|
|
|
import { setValue, valueOf } from './utils' |
|
|
|
|
|
|
|
|
|
|
|
/** 画布配置 */ |
|
|
|
/** 画布配置 */ |
|
|
|
export interface CanvasConfig { |
|
|
|
export interface CanvasConfig { |
|
|
@ -129,17 +129,30 @@ export function useScale() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function useSource<T = unknown>(source: string | undefined, fallback?: T): ComputedRef<T | undefined> { |
|
|
|
export function useSource<T = unknown>(source: string | undefined, fallback?: T): WritableComputedRef<T | undefined> { |
|
|
|
const ctx = useContext() |
|
|
|
const ctx = useContext() |
|
|
|
const blockId = useBlockId() |
|
|
|
const blockId = useBlockId() |
|
|
|
|
|
|
|
|
|
|
|
return computed<T | undefined>(() => { |
|
|
|
return computed<T | undefined>({ |
|
|
|
if (!blockId) { |
|
|
|
get(): T | undefined { |
|
|
|
throw new Error('without block') |
|
|
|
// if (!blockId) {
|
|
|
|
} |
|
|
|
// throw new Error('without block')
|
|
|
|
if (!source) { |
|
|
|
// }
|
|
|
|
return fallback |
|
|
|
const id = blockId || ctx.activeBlockId |
|
|
|
|
|
|
|
if (!source || !id) { |
|
|
|
|
|
|
|
return fallback |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return valueOf(ctx.sources[id], source) ?? fallback |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
set(value: T | undefined) { |
|
|
|
|
|
|
|
const id = blockId || ctx.activeBlockId |
|
|
|
|
|
|
|
// console.log({id, source, value})
|
|
|
|
|
|
|
|
if (source && id) { |
|
|
|
|
|
|
|
if (!ctx.sources[id]) { |
|
|
|
|
|
|
|
ctx.sources[id] = {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
setValue(ctx.sources[id], source, value) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return valueOf(ctx.sources[blockId], source) ?? fallback |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|