diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-20 14:19:21 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-08-20 14:19:21 +0200 |
commit | ee7ea34e6211d3b19a325e4fe8c464c24005bcac (patch) | |
tree | a9b2d3a10031a3f286be75015c3d2d98e99b4f16 /client/src/app/shared | |
parent | 13176a07a95984a53cc59aec5217f2ce9806d1bc (diff) | |
parent | 4ba5632266d0cdd54e8e09ac7416c2aa415eed65 (diff) | |
download | PeerTube-ee7ea34e6211d3b19a325e4fe8c464c24005bcac.tar.gz PeerTube-ee7ea34e6211d3b19a325e4fe8c464c24005bcac.tar.zst PeerTube-ee7ea34e6211d3b19a325e4fe8c464c24005bcac.zip |
Merge branch 'release/1.4.0' into develop
Diffstat (limited to 'client/src/app/shared')
-rw-r--r-- | client/src/app/shared/misc/utils.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/client/src/app/shared/misc/utils.ts b/client/src/app/shared/misc/utils.ts index 85fc1c3a0..f26240d21 100644 --- a/client/src/app/shared/misc/utils.ts +++ b/client/src/app/shared/misc/utils.ts | |||
@@ -134,6 +134,41 @@ function scrollToTop () { | |||
134 | window.scroll(0, 0) | 134 | window.scroll(0, 0) |
135 | } | 135 | } |
136 | 136 | ||
137 | // Thanks: https://github.com/uupaa/dynamic-import-polyfill | ||
138 | function importModule (path: string) { | ||
139 | return new Promise((resolve, reject) => { | ||
140 | const vector = '$importModule$' + Math.random().toString(32).slice(2) | ||
141 | const script = document.createElement('script') | ||
142 | |||
143 | const destructor = () => { | ||
144 | delete window[ vector ] | ||
145 | script.onerror = null | ||
146 | script.onload = null | ||
147 | script.remove() | ||
148 | URL.revokeObjectURL(script.src) | ||
149 | script.src = '' | ||
150 | } | ||
151 | |||
152 | script.defer = true | ||
153 | script.type = 'module' | ||
154 | |||
155 | script.onerror = () => { | ||
156 | reject(new Error(`Failed to import: ${path}`)) | ||
157 | destructor() | ||
158 | } | ||
159 | script.onload = () => { | ||
160 | resolve(window[ vector ]) | ||
161 | destructor() | ||
162 | } | ||
163 | const absURL = (environment.apiUrl || window.location.origin) + path | ||
164 | const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module | ||
165 | const blob = new Blob([ loader ], { type: 'text/javascript' }) | ||
166 | script.src = URL.createObjectURL(blob) | ||
167 | |||
168 | document.head.appendChild(script) | ||
169 | }) | ||
170 | } | ||
171 | |||
137 | export { | 172 | export { |
138 | sortBy, | 173 | sortBy, |
139 | durationToString, | 174 | durationToString, |
@@ -147,5 +182,6 @@ export { | |||
147 | objectToFormData, | 182 | objectToFormData, |
148 | objectLineFeedToHtml, | 183 | objectLineFeedToHtml, |
149 | removeElementFromArray, | 184 | removeElementFromArray, |
185 | importModule, | ||
150 | scrollToTop | 186 | scrollToTop |
151 | } | 187 | } |