Line data Source code
1 : import { applyTextReplacements as _applyTextReplacements } from '@/utils/searchAndReplace'
2 :
3 : import { api } from './api'
4 : import type { LGraph } from '@/lib/litegraph/src/litegraph'
5 : import { $el } from './ui/utils'
6 :
7 4414 : export function clone<T>(obj: T): T {
8 4414 : try {
9 4414 : if (typeof structuredClone !== 'undefined') {
10 4414 : return structuredClone(obj)
11 : }
12 : } catch {
13 : // structuredClone is stricter than using JSON.parse/stringify so fallback to that
14 : }
15 :
16 31 : return JSON.parse(JSON.stringify(obj))
17 : }
18 :
19 : /**
20 : * @deprecated Use `applyTextReplacements` from `@/utils/searchAndReplace` instead
21 : * There are external callers to this function, so we need to keep it for now
22 : */
23 0 : export function applyTextReplacements(
24 : app: { rootGraph: LGraph },
25 : value: string
26 : ): string {
27 0 : return _applyTextReplacements(app.rootGraph, value)
28 : }
29 :
30 0 : export async function addStylesheet(
31 : urlOrFile: string,
32 : relativeTo?: string
33 : ): Promise<void> {
34 0 : return new Promise((res, rej) => {
35 0 : let url
36 0 : if (urlOrFile.endsWith('.js')) {
37 0 : url = urlOrFile.substr(0, urlOrFile.length - 2) + 'css'
38 : } else {
39 0 : url = new URL(
40 : urlOrFile,
41 : relativeTo ?? `${window.location.protocol}//${window.location.host}`
42 : ).toString()
43 : }
44 0 : $el('link', {
45 : parent: document.head,
46 : rel: 'stylesheet',
47 : type: 'text/css',
48 : href: url,
49 : onload: res,
50 : onerror: rej
51 : })
52 : })
53 : }
54 :
55 : export { downloadBlob } from '@/base/common/downloadUtil'
56 :
57 4 : export function uploadFile(accept: string) {
58 4 : return new Promise<File>((resolve, reject) => {
59 4 : const input = document.createElement('input')
60 4 : input.type = 'file'
61 4 : input.accept = accept
62 4 : input.onchange = (e) => {
63 4 : const file = (e.target as HTMLInputElement).files?.[0]
64 4 : if (!file) return reject(new Error('No file selected'))
65 4 : resolve(file)
66 : }
67 4 : input.click()
68 : })
69 : }
70 :
71 7065 : export function prop<T>(
72 : target: object,
73 : name: string,
74 : defaultValue: T,
75 : onChanged?: (
76 : currentValue: T,
77 : previousValue: T,
78 : target: object,
79 : name: string
80 : ) => void
81 : ): T {
82 7065 : let currentValue: T
83 7065 : Object.defineProperty(target, name, {
84 : get() {
85 7065 : return currentValue
86 : },
87 : set(newValue) {
88 7065 : const prevValue = currentValue
89 7065 : currentValue = newValue
90 7065 : onChanged?.(currentValue, prevValue, target, name)
91 : }
92 : })
93 7065 : return defaultValue
94 : }
95 :
96 30 : export function getStorageValue(id: string) {
97 30 : const clientId = api.clientId ?? api.initialClientId
98 30 : return (
99 : (clientId && sessionStorage.getItem(`${id}:${clientId}`)) ??
100 : localStorage.getItem(id)
101 : )
102 : }
103 :
104 0 : export function setStorageValue(id: string, value: string) {
105 0 : const clientId = api.clientId ?? api.initialClientId
106 0 : if (clientId) {
107 0 : sessionStorage.setItem(`${id}:${clientId}`, value)
108 : }
109 0 : localStorage.setItem(id, value)
110 : }
|