vue#onUnmounted TypeScript Examples
The following examples show how to use
vue#onUnmounted.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useBreadcrumbTitle.ts From vite-vue3-ts with MIT License | 6 votes |
useBreadcrumbTitle = (isAddOn = true) => {
const route = useRoute();
const title = ref(route.meta.title);
watch(
() => route.meta.title,
(val) => {
title.value = val;
},
);
const changeTitle = (val: string) => (title.value = val);
onMounted(() => isAddOn && emitter.on(key, changeTitle));
onUnmounted(() => isAddOn && emitter.off(key, changeTitle));
const setBreadcrumbTitle = (title: string) => emitter.emit(key, title);
return {
title,
setBreadcrumbTitle,
};
}
Example #2
Source File: useParent.ts From elenext with MIT License | 6 votes |
useParent = <T>(key: InjectionKey<ParentProvide<T>>) => {
const instance = getCurrentInstance() as ComponentInternalInstance
const parent = inject(key, null)
const index = computed(() => parent?.children.indexOf(instance))
parent?.insert(instance)
onUnmounted(() => {
parent?.remove(instance)
})
return { parent, index }
}
Example #3
Source File: useTree.ts From vue3-treeview with MIT License | 6 votes |
export default function useTree(props: any, emit: (event: string, ...args: any[]) => void): {} {
const element = ref<HTMLElement>(null);
const id = createState(props);
const state = states.get(id);
provide("emitter", emit);
provide("state", state);
const style = computed(() => {
return {
"display": "flex",
"align-items": "center"
};
});
onUnmounted(() => {
states.delete(id);
})
return {
element,
style
};
}
Example #4
Source File: inject.ts From fect with MIT License | 6 votes |
useProvider = <T>(key: InjectionKey<Provider<T>>) => {
const context = inject(key, null)
if (context) {
const instance = getCurrentInstance()!
const { link, unlink, internalChildren, ...rest } = context
link(instance)
onUnmounted(() => unlink(instance))
const idx = computed(() => internalChildren.indexOf(instance))
return {
context: rest,
idx: idx.value
}
}
return {
context: null,
idx: -1
}
}
Example #5
Source File: useResize.ts From jz-gantt with MIT License | 6 votes |
/**
* 监听甘特横向大小变化
*/
export function useResizeGanttObserver() {
const { initGanttWidth, setHeaders } = useSetGanttHeader();
const { ganttRef } = useGanttRef();
const ganttResizeObserver = ref<ResizeObserver>();
onBeforeMount(() => {
ganttResizeObserver.value = new ResizeObserver(entries => {
// eslint-disable-next-line no-restricted-syntax
for (const entry of entries) {
initGanttWidth.value = entry.contentRect.width;
setHeaders();
}
});
});
onMounted(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
ganttRef?.value && ganttResizeObserver.value?.observe(ganttRef.value);
});
onUnmounted(() => {
ganttResizeObserver.value = undefined;
});
return {};
}
Example #6
Source File: useDebounce.ts From vhook with MIT License | 6 votes |
export function useDebounce<T>(value: T, delay = 200) {
let timer: any
const clear = () => {
if (timer) {
clearTimeout(timer)
}
}
if (getCurrentInstance()) {
onUnmounted(() => {
clear()
})
}
return customRef((tracker, trigger) => ({
get() {
tracker()
return value
},
set(val: T) {
clear()
timer = setTimeout(() => {
value = val
timer = null
trigger()
}, delay)
}
}))
}
Example #7
Source File: util.ts From vhook with MIT License | 6 votes |
export function tryOnUnmounted(cb: () => any): void {
if (getCurrentInstance()) {
onUnmounted(cb)
}
}
Example #8
Source File: vue.ts From keen-slider with MIT License | 6 votes |
export function useKeenSlider<
T extends HTMLElement,
O = {},
P = {},
H extends string = KeenSliderHooks
>(
options: Ref<KeenSliderOptions<O, P, H>> | KeenSliderOptions<O, P, H>,
plugins?: KeenSliderPlugin<O, P, H>[]
): [Ref<T | undefined>, Ref<KeenSliderInstance<O, P, H> | undefined>] {
const container = ref<T>()
const slider = ref<KeenSliderInstance<O, P, H>>()
if (isRef(options)) {
watch(options, (newOptions, _) => {
if (slider.value) slider.value.update(newOptions)
})
}
onMounted(() => {
if (container.value)
slider.value = new KeenSlider<O, P, H>(
container.value,
isRef(options) ? options.value : options,
plugins
)
})
onUnmounted(() => {
if (slider.value) slider.value.destroy()
})
return [container, slider]
}
Example #9
Source File: usePlocState.ts From frontend-clean-architecture with MIT License | 6 votes |
export function usePlocState<S>(ploc: Ploc<S>): DeepReadonly<Ref<S>> {
const state = ref(ploc.state) as Ref<S>;
const stateSubscription = (newState: S) => {
state.value = newState;
};
onMounted(() => {
ploc.subscribe(stateSubscription);
});
onUnmounted(() => {
ploc.unsubscribe(stateSubscription);
});
return readonly(state);
}
Example #10
Source File: useAsyncQuery.ts From vue-request with MIT License | 4 votes |
function useAsyncQuery<R, P extends unknown[]>(
query: Query<R, P>,
options: BaseOptions<R, P>,
): BaseResult<R, P> {
const injectedGlobalOptions = inject<GlobalOptions>(
GLOBAL_OPTIONS_PROVIDE_KEY,
{},
);
const {
cacheKey,
defaultParams = ([] as unknown) as P,
manual = false,
ready = ref(true),
refreshDeps = [],
loadingDelay = 0,
pollingWhenHidden = false,
pollingWhenOffline = false,
refreshOnWindowFocus = false,
refocusTimespan = 5000,
cacheTime = 600000,
staleTime = 0,
errorRetryCount = 0,
errorRetryInterval = 0,
queryKey,
...rest
} = {
...getGlobalOptions(),
...injectedGlobalOptions,
...options,
};
const stopPollingWhenHiddenOrOffline = ref(false);
// skip debounce when initail run
const initialAutoRunFlag = ref(false);
const updateCache = (state: State<R, P>) => {
if (!cacheKey) return;
const cacheData = getCache<R, P>(cacheKey)?.data;
const cacheQueries = cacheData?.queries;
const queryData = unRefObject(state);
const currentQueryKey =
queryKey?.(...state.params.value) ?? QUERY_DEFAULT_KEY;
setCache<R, P>(
cacheKey,
{
queries: {
...cacheQueries,
[currentQueryKey]: {
...cacheQueries?.[currentQueryKey],
...queryData,
},
},
latestQueriesKey: currentQueryKey,
},
cacheTime,
);
};
const config = {
initialAutoRunFlag,
loadingDelay,
pollingWhenHidden,
pollingWhenOffline,
stopPollingWhenHiddenOrOffline,
cacheKey,
errorRetryCount,
errorRetryInterval,
refreshOnWindowFocus,
refocusTimespan,
updateCache,
...omit(rest, ['pagination', 'listKey']),
} as Config<R, P>;
const loading = ref(false);
const data = ref<R>();
const error = ref<Error>();
const params = ref() as Ref<P>;
const queries = <Queries<R, P>>reactive({
[QUERY_DEFAULT_KEY]: reactive(createQuery(query, config)),
});
const latestQueriesKey = ref(QUERY_DEFAULT_KEY);
const latestQuery = computed(() => queries[latestQueriesKey.value] ?? {});
// sync state
watch(
latestQuery,
queryData => {
loading.value = queryData.loading;
data.value = queryData.data;
error.value = queryData.error;
params.value = queryData.params;
},
{
immediate: true,
deep: true,
},
);
// init queries from cache
if (cacheKey) {
const cache = getCache<R, P>(cacheKey);
if (cache?.data?.queries) {
Object.keys(cache.data.queries).forEach(key => {
const cacheQuery = cache.data.queries![key];
queries[key] = <UnWrapState<R, P>>reactive(
createQuery(query, config, {
loading: cacheQuery.loading,
params: cacheQuery.params,
data: cacheQuery.data,
error: cacheQuery.error,
}),
);
});
/* istanbul ignore else */
if (cache.data.latestQueriesKey) {
latestQueriesKey.value = cache.data.latestQueriesKey;
}
}
}
const tempReadyParams = ref();
const hasTriggerReady = ref(false);
const run = (...args: P) => {
if (!ready.value && !hasTriggerReady.value) {
tempReadyParams.value = args;
return resolvedPromise;
}
const newKey = queryKey?.(...args) ?? QUERY_DEFAULT_KEY;
if (!queries[newKey]) {
queries[newKey] = <UnWrapState<R, P>>reactive(createQuery(query, config));
}
latestQueriesKey.value = newKey;
return latestQuery.value.run(...args);
};
const reset = () => {
unmountQueries();
latestQueriesKey.value = QUERY_DEFAULT_KEY;
queries[QUERY_DEFAULT_KEY] = <UnWrapState<R, P>>(
reactive(createQuery(query, config))
);
};
// unmount queries
const unmountQueries = () => {
Object.keys(queries).forEach(key => {
queries[key].cancel();
queries[key].unmount();
delete queries[key];
});
};
const cancel = () => latestQuery.value.cancel();
const refresh = () => latestQuery.value.refresh();
const mutate = <Mutate<R>>((arg: R) => latestQuery.value.mutate(arg));
// initial run
if (!manual) {
initialAutoRunFlag.value = true;
// TODO: need refactor
const cache = getCache<R, P>(cacheKey!);
const cacheQueries = cache?.data.queries ?? {};
const isFresh =
cache &&
(staleTime === -1 || cache.cacheTime + staleTime > new Date().getTime());
const hasCacheQueries = Object.keys(cacheQueries).length > 0;
if (!isFresh) {
if (hasCacheQueries) {
Object.keys(queries).forEach(key => {
queries[key]?.refresh();
});
} else {
run(...defaultParams);
}
}
initialAutoRunFlag.value = false;
}
// watch ready
const stopReady = ref();
stopReady.value = watch(
ready,
val => {
hasTriggerReady.value = true;
if (val && tempReadyParams.value) {
run(...tempReadyParams.value);
// destroy current watch
stopReady.value();
}
},
{
flush: 'sync',
},
);
// watch refreshDeps
if (refreshDeps.length) {
watch(refreshDeps, () => {
!manual && latestQuery.value.refresh();
});
}
onUnmounted(() => {
unmountQueries();
});
return {
loading,
data,
error,
params,
cancel,
refresh,
mutate,
run,
reset,
queries,
};
}
Example #11
Source File: index.ts From elenext with MIT License | 4 votes |
usePopper = (props: UsePopperOptions) => {
const popperId = uniqueId('el-popper')
const { referenceRef, popperRef } = props
const timers: {
showTimer: any
hideTimer: any
} = { showTimer: undefined, hideTimer: undefined }
const state = reactive<usePopperState>({
instance: null,
popperId,
attrs: {
styles: {
popper: {
position: 'absolute',
left: '0',
top: '0',
},
arrow: {
position: 'absolute',
},
},
attributes: {},
},
})
const popperOptions = computed<PopperOptions>(() => {
return {
placement: props.placement || 'bottom-start',
strategy: 'absolute',
modifiers: [
{
name: 'updateState',
enabled: true,
phase: 'write',
fn: ({ state: popperState }: any) => {
const elements = Object.keys(popperState.elements)
state.attrs = {
styles: fromEntries(elements.map(element => [element, popperState.styles[element] || {}])),
attributes: fromEntries(elements.map(element => [element, popperState.attributes[element] || {}])),
}
},
requires: ['computeStyles'],
},
{ name: 'applyStyles', enabled: false },
{ name: 'offset', options: { offset: [0, props.offset || 0] } },
],
}
})
const clearScheduled = () => {
clearTimeout(timers.hideTimer)
clearTimeout(timers.showTimer)
}
let clickEvent: any = null
const togglePopper = (event: MouseEvent) => {
clickEvent = event
props.onTrigger(popperId)
}
const showPopper = () => {
clearScheduled()
timers.showTimer = setTimeout(() => {
props.onTrigger(popperId, true)
}, 0)
}
const hidePopper = () => {
clearScheduled()
timers.hideTimer = setTimeout(() => {
props.onTrigger(popperId, false)
}, props.hideDaly || 200)
}
const outSideClickHandler = (event: MouseEvent) => {
// outSideClick 和 togglePopper 冲突
if (event === clickEvent) {
return
}
if (popperRef.value && !popperRef.value.contains(event.target as Node)) {
if (
['hover', 'focus'].indexOf(props.trigger) !== -1 &&
referenceRef.value &&
referenceRef.value.contains(event.target as Node)
) {
return
} else {
hidePopper()
}
}
}
const eventRegOrUnReg = isReg => {
const referenceEl = referenceRef.value
const popperEl = popperRef.value
const event = isReg ? 'addEventListener' : 'removeEventListener'
if (referenceEl && popperEl) {
if (props.trigger === 'hover') {
referenceEl[event]('mouseenter', showPopper)
referenceEl[event]('mouseleave', hidePopper)
popperEl[event]('mouseenter', showPopper)
popperEl[event]('mouseleave', hidePopper)
}
if (props.trigger === 'click') {
referenceEl[event]('click', togglePopper)
// popperEl[event]('mouseenter', showPopper)
// popperEl[event]('mouseleave', hidePopper)
}
if (props.trigger === 'focus') {
referenceEl[event]('focus', showPopper)
referenceEl[event]('blur', hidePopper)
}
if (props.trigger !== 'manual') {
document[event]('click', outSideClickHandler)
}
}
}
watchEffect(() => {
if (state.instance) {
state.instance.setOptions(popperOptions.value)
}
})
watch([referenceRef, popperRef], () => {
const referenceEl = referenceRef.value
const popperEl = popperRef.value
if (referenceEl && popperEl) {
if (state.instance) {
state.instance.destroy()
}
state.instance = createPopper(referenceEl, popperEl as HTMLElement, popperOptions.value)
}
})
watchEffect(onInvalidate => {
const referenceEl = referenceRef.value
const popperEl = popperRef.value
onInvalidate(() => {
eventRegOrUnReg(false)
})
if (referenceEl && popperEl) {
eventRegOrUnReg(true)
}
})
onUnmounted(() => {
if (state.instance) {
state.instance.destroy()
}
})
return state
}
Example #12
Source File: useNode.ts From vue3-treeview with MIT License | 4 votes |
export function useNode(cmn: IUseCommon, props: INodeProps): IUseNode {
const state = cmn.state;
const node = cmn.node;
const config = cmn.config;
const wrapper = cmn.wrapper;
const editing = cmn.editing;
const level = ref(null);
const depth = ref(props.depth);
const index = ref(props.index);
const id = computed(() => {
return hasNode.value && node.value.id;
});
const hasNode = computed(() => {
return !isNil(node);
});
const hasState = computed(() => {
return hasNode.value && !isNil(node.value.state);
});
const roots = computed(() => {
return config.value.roots || [];
});
const children = computed(() => {
return isNil(node.value.children) ? [] : node.value.children;
});
const nbChildren = computed(() => {
return children.value.length;
});
const hasChildren = computed(() => {
return nbChildren.value > 0;
});
const opened = computed(() => {
return hasState.value && node.value.state.opened || false;
});
const isLoading = computed(() => {
return hasState.value && node.value.state.isLoading || false;
});
const displayLoading = computed(() => {
return isLoading.value && !hasChildren.value && opened.value;
});
const displayLevel = computed(() => {
return !isLoading.value && hasChildren.value && opened.value;
});
const style = computed(() => {
return {
display: "flex"
};
});
const disabledClass = computed(() => {
if (!cmn.disabled.value) {
return null;
}
return config.value.disabledClass ? config.value.disabledClass : defaultDisabledClass;
});
const hideIcons = computed(() => {
for (const id of roots.value) {
const node = state.nodes.value[id];
if (node.children && node.children.length > 0) {
return false;
}
}
return true;
});
const isLeaf = computed(() => {
if (isArray(config.value.leaves)) {
const arr: string[] = config.value.leaves;
const idx = arr.indexOf(id.value);
return Number.isFinite(idx) && idx >= 0;
}
return isNil(node.value.children) || !isArray(node.value.children) || node.value.children.length === 0;
});
const isFocusable = computed(() => {
return state.focusable.value === node.value.id;
});
const tabIndex = computed(() => {
if (depth.value === 0 && index.value === 0 && isNil(state.focusable.value)) {
return 0;
}
return isFocusable.value ? 0 : -1;
});
const focusClass = computed(() => {
if (!cmn.focused.value) {
return null;
}
return config.value.focusClass ? config.value.focusClass : defaultFocusClass;
});
watch(opened, (nv: boolean) => {
nv ? cmn.root.emit(nodeEvents.opened, node.value) : cmn.root.emit(nodeEvents.closed, node.value);
});
const focus = (() => {
state.focusable.value = node.value.id;
nextTick(() => {
wrapper.value.focus();
cmn.focused.value = true;
cmn.root.emit(nodeEvents.focus, node.value);
});
});
const toggle = (() => {
node.value.state.opened = !node.value.state.opened;
cmn.root.emit(nodeEvents.toggle, node.value);
});
const right = (() => {
if (!editing.value && config.value.keyboardNavigation) {
node.value.state.opened = true;
}
});
const left = (() => {
if (!editing.value && config.value.keyboardNavigation) {
node.value.state.opened = false;
}
});
const move = ((getFunc: (s: string) => string) => {
const id = getFunc(node.value.id);
if (!isNil(id) && config.value.keyboardNavigation) {
const f = state.focusFunc.get(id);
if(f) {
f();
}
}
});
const up = () => move(prevVisible);
const prev = ((id: string): string => {
const n = state.nodes.value[id];
if (n.children && n.children.length > 0) {
const idx = n.children.indexOf(node.value.id);
const prev = n.children[idx - 1];
if (!isNil(prev)) {
return lastChild(prev);
}
}
return n.id;
});
const prevVisible = ((id: string) => {
const n = state.nodes.value[id];
const p = state.nodes.value[n.parent];
if (!p) {
const idx = roots.value.indexOf(id);
return lastChild(roots.value[idx - 1]) || null;
}
return prev(p.id);
});
const lastChild = ((id: string): string => {
const n = state.nodes.value[id];
if (!n) {
return null;
}
if (n.children && n.children.length > 0 && n.state.opened) {
const last = n.children[n.children.length - 1];
if (!isNil(last)) {
return lastChild(last);
}
}
return n.id;
});
const down = () => move(nextVisible);
const nextRoot = ((id: string) => {
const idx = roots.value.indexOf(id);
return roots.value[idx + 1] || null;
});
const next = ((p: INode, id: string): string => {
const idx = p.children.indexOf(id);
if (p.children[idx + 1]) {
return p.children[idx + 1];
}
if (p.parent) {
return next(state.nodes.value[p.parent], p.id);
}
return nextRoot(p.id);
});
const nextVisible = ((id: string): string => {
const n = state.nodes.value[id];
if (n.children && n.children.length > 0 && n.state.opened) {
return n.children[0];
}
const p = state.nodes.value[n.parent];
return p ? next(p, id) : nextRoot(id);
});
onMounted(() => {
state.focusFunc.set(node.value.id, focus);
});
onUnmounted(() => {
state.focusFunc.delete(node.value.id);
});
return {
id,
level,
style,
opened,
hasNode,
hideIcons,
hasChildren,
tabIndex,
focusClass,
disabledClass,
isLeaf,
isLoading,
displayLoading,
displayLevel,
right,
left,
up,
down,
toggle,
focus,
prevVisible,
nextVisible
};
}
Example #13
Source File: useInput.ts From formkit with MIT License | 4 votes |
/**
* A composable for creating a new FormKit node.
* @param type - The type of node (input, group, list)
* @param attrs - The FormKit "props" — which is really the attrs list.
* @returns
* @public
*/
export function useInput(
props: FormKitComponentProps,
context: SetupContext<any>,
options: FormKitOptions = {}
): FormKitNode {
/**
* The configuration options, these are provided by either the plugin or by
* explicit props.
*/
const config = Object.assign({}, inject(optionsSymbol) || {}, options)
/**
* The current instance.
*/
const instance = getCurrentInstance()
/**
* Extracts the listeners.
*/
const listeners = onlyListeners(instance?.vnode.props)
/**
* Determines if the prop is v-modeled.
*/
const isVModeled = props.modelValue !== undefined
/**
* Determines if the object being passed as a v-model is reactive.
*/
// const isReactiveVModel = isVModeled && isReactive(props.modelValue)
/**
* Define the initial component
*/
const value: any =
props.modelValue !== undefined
? props.modelValue
: cloneAny(context.attrs.value)
/**
* Creates the node's initial props from the context, props, and definition
* @returns
*/
function createInitialProps(): Record<string, any> {
const initialProps: Record<string, any> = {
...nodeProps(props),
...listeners,
}
const attrs = except(nodeProps(context.attrs), pseudoProps)
initialProps.attrs = attrs
const propValues = only(nodeProps(context.attrs), pseudoProps)
for (const propName in propValues) {
initialProps[camel(propName)] = propValues[propName]
}
const classesProps = { props: {} }
classesToNodeProps(classesProps as FormKitNode, props)
Object.assign(initialProps, classesProps.props)
if (typeof initialProps.type !== 'string') {
initialProps.definition = initialProps.type
delete initialProps.type
}
return initialProps
}
/**
* Create the FormKitNode.
*/
const initialProps = createInitialProps()
/**
* The parent node.
*/
const parent = initialProps.ignore
? null
: props.parent || inject(parentSymbol, null)
const node = createNode(
extend(
config || {},
{
name: props.name || undefined,
value,
parent,
plugins: (config.plugins || []).concat(props.plugins),
config: props.config,
props: initialProps,
index: props.index,
},
false,
true
) as Partial<FormKitOptions>
) as FormKitNode
/**
* If no definition has been assigned at this point — we're out!
*/
if (!node.props.definition) error(600, node)
/**
* All props that are bound "late" (after node creation) — are added to a set
* which is used to watch the context.attrs object.
*/
const lateBoundProps = ref<Set<string | RegExp>>(
new Set(node.props.definition.props || [])
)
/**
* Any additional props added at a "later" time should also be part of the
* late bound props.
*/
node.on('added-props', ({ payload: lateProps }) => {
if (Array.isArray(lateProps))
lateProps.forEach((newProp) => lateBoundProps.value.add(newProp))
})
/**
* These prop names must be assigned.
*/
const pseudoPropNames = computed(() =>
pseudoProps.concat([...lateBoundProps.value]).reduce((names, prop) => {
if (typeof prop === 'string') {
names.push(camel(prop))
names.push(kebab(prop))
} else {
names.push(prop)
}
return names
}, [] as Array<string | RegExp>)
)
/* Splits Classes object into discrete props for each key */
watchEffect(() => classesToNodeProps(node, props))
/**
* The props object already has properties even if they start as "undefined"
* so we can loop over them and individual watchEffect to prevent responding
* inappropriately.
*/
const passThrough = nodeProps(props)
for (const prop in passThrough) {
watch(
() => props[prop as keyof FormKitComponentProps],
() => {
if (props[prop as keyof FormKitComponentProps] !== undefined) {
node.props[prop] = props[prop as keyof FormKitComponentProps]
}
}
)
}
/**
* Watch "pseudoProp" attributes explicitly.
*/
const attributeWatchers = new Set<WatchStopHandle>()
const possibleProps = nodeProps(context.attrs)
watchEffect(() => {
watchAttributes(only(possibleProps, pseudoPropNames.value))
})
/**
* Defines attributes that should be used as props.
* @param attrProps - Attributes that should be used as props instead
*/
function watchAttributes(attrProps: Record<string, any>) {
attributeWatchers.forEach((stop) => {
stop()
attributeWatchers.delete(stop)
})
for (const prop in attrProps) {
const camelName = camel(prop)
attributeWatchers.add(
watch(
() => context.attrs[prop],
() => {
node.props[camelName] = context.attrs[prop]
}
)
)
}
}
/**
* Watch and dynamically set attribute values, those values that are not
* props and are not pseudoProps
*/
watchEffect(() => {
const attrs = except(nodeProps(context.attrs), pseudoPropNames.value)
node.props.attrs = Object.assign({}, node.props.attrs || {}, attrs)
})
/**
* Add any/all "prop" errors to the store.
*/
watchEffect(() => {
const messages = props.errors.map((error) =>
createMessage({
key: slugify(error),
type: 'error',
value: error,
meta: { source: 'prop' },
})
)
node.store.apply(
messages,
(message) => message.type === 'error' && message.meta.source === 'prop'
)
})
/**
* Add input errors.
*/
if (node.type !== 'input') {
const sourceKey = `${node.name}-prop`
watchEffect(() => {
const keys = Object.keys(props.inputErrors)
const messages = keys.reduce((messages, key) => {
let value = props.inputErrors[key]
if (typeof value === 'string') value = [value]
if (Array.isArray(value)) {
messages[key] = value.map((error) =>
createMessage({
key: error,
type: 'error',
value: error,
meta: { source: sourceKey },
})
)
}
return messages
}, {} as Record<string, FormKitMessage[]>)
node.store.apply(
messages,
(message) =>
message.type === 'error' && message.meta.source === sourceKey
)
})
}
/**
* Watch the config prop for any changes.
*/
watchEffect(() => Object.assign(node.config, props.config))
/**
* Produce another parent object.
*/
if (node.type !== 'input') {
provide(parentSymbol, node)
}
let inputTimeout: number | undefined
// eslint-disable-next-line @typescript-eslint/ban-types
const mutex = new WeakSet<object>()
/**
* Explicitly watch the input value, and emit changes (lazy)
*/
node.on('modelUpdated', () => {
// Emit the values after commit
context.emit('inputRaw', node.context?.value, node)
clearTimeout(inputTimeout)
inputTimeout = setTimeout(
context.emit,
20,
'input',
node.context?.value,
node
) as unknown as number
if (isVModeled && node.context) {
const newValue = useRaw(node.context.value)
if (isObject(newValue) && useRaw(props.modelValue) !== newValue) {
// If this is an object that has been mutated inside FormKit core then
// we know when it is emitted it will "return" in the watchVerbose so
// we pro-actively add it to the mutex.
mutex.add(newValue)
}
context.emit('update:modelValue', newValue)
}
})
/**
* Enabled support for v-model, using this for groups/lists is not recommended
*/
if (isVModeled) {
watchVerbose(toRef(props, 'modelValue'), (path, value): void | boolean => {
const rawValue = useRaw(value)
if (isObject(rawValue) && mutex.has(rawValue)) {
return mutex.delete(rawValue)
}
if (!path.length) node.input(value, false)
else node.at(path)?.input(value, false)
})
}
/**
* When this input shuts down, we need to "delete" the node too.
*/
onUnmounted(() => node.destroy())
return node
}
Example #14
Source File: i18n.ts From vue-i18n-next with MIT License | 4 votes |
function setupLifeCycle(
i18n: I18nInternal,
target: ComponentInternalInstance,
composer: Composer
): void {
let emitter: VueDevToolsEmitter | null = null
if (__BRIDGE__) {
// assign legacy VueI18n instance to Vue2 instance
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const vm = target.proxy as any
if (vm == null) {
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _i18n = (composer as any)[LegacyInstanceSymbol]
if (_i18n === i18n) {
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR)
}
vm._i18n = _i18n
vm._i18n_bridge = true
// browser only
if (inBrowser) {
vm._i18nWatcher = vm._i18n.watchI18nData()
if (vm._i18n._sync) {
vm._localeWatcher = vm._i18n.watchLocale()
}
}
let subscribing = false
onBeforeMount(() => {
vm._i18n.subscribeDataChanging(vm)
subscribing = true
}, target)
onUnmounted(() => {
if (subscribing) {
vm._i18n.unsubscribeDataChanging(vm)
subscribing = false
}
if (vm._i18nWatcher) {
vm._i18nWatcher()
vm._i18n.destroyVM()
delete vm._i18nWatcher
}
if (vm._localeWatcher) {
vm._localeWatcher()
delete vm._localeWatcher
}
delete vm._i18n_bridge
delete vm._i18n
}, target)
} else {
onMounted(() => {
// inject composer instance to DOM for intlify-devtools
if (
(__DEV__ || __FEATURE_PROD_VUE_DEVTOOLS__) &&
!__NODE_JS__ &&
target.vnode.el
) {
target.vnode.el.__VUE_I18N__ = composer
emitter = createEmitter<VueDevToolsEmitterEvents>()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _composer = composer as any
_composer[EnableEmitter] && _composer[EnableEmitter](emitter)
emitter.on('*', addTimelineEvent)
}
}, target)
onUnmounted(() => {
// remove composer instance from DOM for intlify-devtools
if (
(__DEV__ || __FEATURE_PROD_VUE_DEVTOOLS__) &&
!__NODE_JS__ &&
target.vnode.el &&
target.vnode.el.__VUE_I18N__
) {
emitter && emitter.off('*', addTimelineEvent)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _composer = composer as any
_composer[DisableEmitter] && _composer[DisableEmitter]()
delete target.vnode.el.__VUE_I18N__
}
i18n.__deleteInstance(target)
}, target)
}
}
Example #15
Source File: EventMonitoring.ts From screen-shot with MIT License | 4 votes |
constructor(props: Record<string, any>, context: SetupContext<any>) {
// 实例化响应式data
this.data = new InitData();
// 获取截图区域canvas容器
this.screenShortController = this.data.getScreenShortController();
this.toolController = this.data.getToolController();
this.textInputController = this.data.getTextInputController();
this.optionController = this.data.getOptionController();
this.optionIcoController = this.data.getOptionIcoController();
this.videoController = document.createElement("video");
this.videoController.autoplay = true;
this.screenShortImageController = document.createElement("canvas");
// 设置实例与属性
this.data.setPropsData(context.emit);
onMounted(() => {
this.emit = this.data.getEmit();
const plugInParameters = new PlugInParameters();
// 单击截屏启用状态
this.clickCutFullScreen = plugInParameters.getClickCutFullScreenStatus();
// 设置需要隐藏的工具栏图标
this.data.setHiddenToolIco(plugInParameters.getHiddenToolIco());
// 设置截图区域canvas宽高
this.data.setScreenShortInfo(window.innerWidth, window.innerHeight);
if (this.screenShortImageController == null) return;
// 设置截图图片存放容器宽高
this.screenShortImageController.width = window.innerWidth;
this.screenShortImageController.height = window.innerHeight;
if (plugInParameters.getWebRtcStatus()) {
// 设置为屏幕宽高
this.data.setScreenShortInfo(window.screen.width, window.screen.height);
// 设置为屏幕宽高
this.screenShortImageController.width = window.screen.width;
this.screenShortImageController.height = window.screen.height;
}
// 获取截图区域画canvas容器画布
const context = this.screenShortController.value?.getContext("2d");
if (context == null) return;
if (!plugInParameters.getWebRtcStatus()) {
// html2canvas截屏
html2canvas(document.body, {
useCORS: plugInParameters.getEnableCORSStatus(),
proxy: plugInParameters.getProxyAddress()
}).then(canvas => {
// 装载截图的dom为null则退出
if (this.screenShortController.value == null) return;
// 调整截屏容器层级
this.screenShortController.value.style.zIndex =
plugInParameters.getLevel() + "";
// 调整截图工具栏层级
if (this.toolController.value == null) return;
this.toolController.value.style.zIndex = `${plugInParameters.getLevel() +
1}`;
// 存放html2canvas截取的内容
this.screenShortImageController = canvas;
// 存储屏幕截图
this.data.setScreenShortImageController(canvas);
// 赋值截图区域canvas画布
this.screenShortCanvas = context;
// 绘制蒙层
drawMasking(context);
// 添加监听
this.screenShortController.value?.addEventListener(
"mousedown",
this.mouseDownEvent
);
this.screenShortController.value?.addEventListener(
"mousemove",
this.mouseMoveEvent
);
this.screenShortController.value?.addEventListener(
"mouseup",
this.mouseUpEvent
);
});
return;
}
// 开始捕捉屏幕
this.startCapture().then(() => {
setTimeout(() => {
// 装载截图的dom为null则退出
if (this.screenShortController.value == null) return;
if (this.screenShortImageController == null) return;
const imageContext = this.screenShortImageController.getContext("2d");
if (imageContext == null) return;
// 将获取到的屏幕截图绘制到图片容器里
imageContext.drawImage(
this.videoController,
0,
0,
this.screenShortImageController.width,
this.screenShortImageController.height
);
// 存储屏幕截图
this.data.setScreenShortImageController(
this.screenShortImageController
);
// 赋值截图区域canvas画布
this.screenShortCanvas = context;
// 绘制蒙层
drawMasking(context);
// 添加监听
this.screenShortController.value?.addEventListener(
"mousedown",
this.mouseDownEvent
);
this.screenShortController.value?.addEventListener(
"mousemove",
this.mouseMoveEvent
);
this.screenShortController.value?.addEventListener(
"mouseup",
this.mouseUpEvent
);
// 停止捕捉屏幕
this.stopCapture();
// 调整截屏容器层级
this.screenShortController.value.style.zIndex =
plugInParameters.getLevel() + "";
// 调整截图工具栏层级
if (this.toolController.value == null) return;
this.toolController.value.style.zIndex = `${plugInParameters.getLevel() +
1}`;
}, 500);
});
});
onUnmounted(() => {
// 初始化initData中的数据
this.data.setInitStatus(true);
});
}
Example #16
Source File: watch.ts From theila with Mozilla Public License 2.0 | 4 votes |
// setup is meant to be called from the component setup method
// and provides seamless integration with the reactive properties of the component.
public setup(props: any, componentContext: any) {
const {
resource,
context,
kubernetes,
talos,
theila,
compareFn,
} = toRefs(props);
watch([
resource,
context,
kubernetes,
talos,
theila,
compareFn,
ctx.current,
], (val, oldVal) => {
if(JSON.stringify(val) != JSON.stringify(oldVal)) {
startWatch();
}
});
const startWatch = async () => {
stopWatch();
if(!resource.value) {
return;
}
let source:Runtime;
if(kubernetes.value) {
source = Runtime.Kubernetes;
} else if(talos.value) {
source = Runtime.Talos;
} else if(theila.value) {
source = Runtime.Theila;
}
else {
throw new Error("unknown source specified");
}
const compare = compareFn && compareFn.value ? compareFn.value : defaultCompareFunc(this);
const c = {};
if(context && context.value) {
Object.assign(c, context.value)
}
// override the context name by the current default one unless it's explicitly defined
if(!componentContext.context || !componentContext.context.name)
c["name"] = contextName();
await this.start(
source,
resource.value,
c,
compare,
);
};
const stopWatch = async () => {
if(this.running.value) {
await this.stop();
}
};
const handleReconnect = async () => {
await startWatch();
}
onMounted(async () => {
ctx.api.addListener(ClientReconnected, handleReconnect);
await startWatch();
});
onUnmounted(async () => {
ctx.api.removeListener(ClientReconnected, handleReconnect);
await stopWatch();
});
}