@file-viewer/renderer-media 3.3.3 → 3.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/audio.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createFileViewerTranslator } from '@file-viewer/core';
1
+ import { createFileViewerTranslator, createFileViewerI18nBindings } from '@file-viewer/core';
2
2
  import { getFileViewerMidiLoader } from './optionalCapabilities.js';
3
3
  const AUDIO_MIME_MAP = {
4
4
  aac: 'audio/aac',
@@ -85,29 +85,44 @@ const createRenderedInstance = (target, cleanup) => ({
85
85
  }
86
86
  });
87
87
  const renderAudioElement = (buffer, target, type, context) => {
88
- const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
88
+ const document = target.ownerDocument;
89
+ const bindings = createFileViewerI18nBindings(context === null || context === void 0 ? void 0 : context.i18n);
90
+ const element = (tag, className, text) => {
91
+ const node = document.createElement(tag);
92
+ if (className)
93
+ node.className = className;
94
+ if (text !== undefined)
95
+ node.textContent = text;
96
+ return node;
97
+ };
98
+ const owned = (tag, className, value) => {
99
+ const node = element(tag, className);
100
+ bindings.text(node, value);
101
+ return node;
102
+ };
103
+ const t = createFileViewerTranslator((context === null || context === void 0 ? void 0 : context.i18n) || (context === null || context === void 0 ? void 0 : context.options));
89
104
  const normalizedType = type.trim().toLowerCase() || 'mp3';
90
105
  const mimeType = AUDIO_MIME_MAP[normalizedType] || 'audio/*';
91
106
  const sourceUrl = URL.createObjectURL(new Blob([buffer], { type: mimeType }));
92
- const root = createElement('div', 'fv-audio-viewer');
93
- const card = createElement('section', 'fv-audio-card');
94
- const art = createElement('div', 'fv-audio-art');
95
- art.append(createElement('span'), createElement('i'));
96
- const copy = createElement('div', 'fv-audio-copy');
97
- copy.append(createElement('span', 'fv-audio-kicker', normalizedType.toUpperCase() || 'AUDIO'), createElement('strong', '', t('media.audio.title')), createElement('p', '', t('media.audio.description')));
98
- const currentTimeText = createElement('span', '', '00:00');
99
- const durationText = createElement('span', '', '--:--');
100
- const progressFill = createElement('i');
101
- const progress = createElement('div', 'fv-audio-progress');
107
+ const root = element('div', 'fv-audio-viewer');
108
+ const card = element('section', 'fv-audio-card');
109
+ const art = element('div', 'fv-audio-art');
110
+ art.append(element('span'), element('i'));
111
+ const copy = element('div', 'fv-audio-copy');
112
+ copy.append(element('span', 'fv-audio-kicker', normalizedType.toUpperCase() || 'AUDIO'), owned('strong', '', () => t('media.audio.title')), owned('p', '', () => t('media.audio.description')));
113
+ const currentTimeText = element('span', '', '00:00');
114
+ const durationText = element('span', '', '--:--');
115
+ const progressFill = element('i');
116
+ const progress = element('div', 'fv-audio-progress');
102
117
  progress.setAttribute('aria-hidden', 'true');
103
118
  progress.append(progressFill);
104
- const meter = createElement('div', 'fv-audio-meter');
119
+ const meter = element('div', 'fv-audio-meter');
105
120
  meter.append(currentTimeText, progress, durationText);
106
- const audio = createElement('audio', 'fv-audio-control');
121
+ const audio = element('audio', 'fv-audio-control');
107
122
  audio.src = sourceUrl;
108
123
  audio.controls = true;
109
124
  audio.preload = 'metadata';
110
- audio.textContent = t('media.audio.unsupported');
125
+ bindings.text(audio, () => t('media.audio.unsupported'));
111
126
  const handleLoadedMetadata = () => {
112
127
  durationText.textContent = Number.isFinite(audio.duration) && audio.duration > 0
113
128
  ? formatDuration(audio.duration)
@@ -124,8 +139,11 @@ const renderAudioElement = (buffer, target, type, context) => {
124
139
  audio.addEventListener('timeupdate', handleTimeUpdate);
125
140
  card.append(art, copy, meter, audio);
126
141
  root.append(card);
127
- target.replaceChildren(createStyle(), root);
142
+ const style = document.createElement('style');
143
+ style.textContent = audioStyle;
144
+ target.replaceChildren(style, root);
128
145
  return createRenderedInstance(target, () => {
146
+ bindings.destroy();
129
147
  audio.pause();
130
148
  audio.removeEventListener('loadedmetadata', handleLoadedMetadata);
131
149
  audio.removeEventListener('timeupdate', handleTimeUpdate);
@@ -133,40 +151,62 @@ const renderAudioElement = (buffer, target, type, context) => {
133
151
  });
134
152
  };
135
153
  const renderMidiElement = (buffer, target, context) => {
136
- const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
154
+ const document = target.ownerDocument;
155
+ const bindings = createFileViewerI18nBindings(context === null || context === void 0 ? void 0 : context.i18n);
156
+ const element = (tag, className, text) => {
157
+ const node = document.createElement(tag);
158
+ if (className)
159
+ node.className = className;
160
+ if (text !== undefined)
161
+ node.textContent = text;
162
+ return node;
163
+ };
164
+ const owned = (tag, className, value) => {
165
+ const node = element(tag, className);
166
+ bindings.text(node, value);
167
+ return node;
168
+ };
169
+ const t = createFileViewerTranslator((context === null || context === void 0 ? void 0 : context.i18n) || (context === null || context === void 0 ? void 0 : context.options));
137
170
  let disposed = false;
138
- const root = createElement('div', 'fv-midi-viewer');
139
- const card = createElement('section', 'fv-midi-card');
171
+ let ownedErrorKey;
172
+ const root = element('div', 'fv-midi-viewer');
173
+ const card = element('section', 'fv-midi-card');
140
174
  const header = document.createElement('header');
141
- const title = createElement('strong', '', t('media.midi.title'));
142
- header.append(createElement('span', '', 'MIDI'), title);
143
- const body = createElement('div', 'fv-midi-state', t('media.midi.loading'));
175
+ const title = owned('strong', '', () => t('media.midi.title'));
176
+ header.append(element('span', '', 'MIDI'), title);
177
+ const body = owned('div', 'fv-midi-state', () => t('media.midi.loading'));
144
178
  card.append(header, body);
145
179
  root.append(card);
146
- target.replaceChildren(createStyle(), root);
147
- const renderError = (message) => {
180
+ const style = document.createElement('style');
181
+ style.textContent = audioStyle;
182
+ target.replaceChildren(style, root);
183
+ const renderError = (message, ownedFallback = false) => {
184
+ bindings.release(body);
148
185
  body.className = 'fv-midi-state fv-midi-error';
149
- body.textContent = message;
186
+ if (ownedFallback)
187
+ bindings.text(body, () => t(ownedErrorKey || 'media.midi.parseFailed'));
188
+ else
189
+ body.textContent = message;
150
190
  };
151
191
  const renderTrackTable = (tracks) => {
152
- const wrap = createElement('div', 'fv-midi-table-wrap');
153
- const table = createElement('table', 'fv-midi-table');
192
+ const wrap = element('div', 'fv-midi-table-wrap');
193
+ const table = element('table', 'fv-midi-table');
154
194
  const thead = document.createElement('thead');
155
195
  const headerRow = document.createElement('tr');
156
196
  for (const label of [
157
- t('media.midi.trackHeader'),
158
- t('media.midi.instrumentHeader'),
159
- t('media.midi.channelHeader'),
160
- t('media.midi.noteCountHeader'),
161
- t('media.midi.durationHeader')
197
+ 'media.midi.trackHeader',
198
+ 'media.midi.instrumentHeader',
199
+ 'media.midi.channelHeader',
200
+ 'media.midi.noteCountHeader',
201
+ 'media.midi.durationHeader'
162
202
  ]) {
163
- headerRow.append(createElement('th', '', label));
203
+ headerRow.append(owned('th', '', () => t(label)));
164
204
  }
165
205
  thead.append(headerRow);
166
206
  const tbody = document.createElement('tbody');
167
- for (const track of tracks) {
207
+ for (const [index, track] of tracks.entries()) {
168
208
  const row = document.createElement('tr');
169
- row.append(createElement('td', '', track.name), createElement('td', '', track.instrument), createElement('td', '', String(track.channel + 1)), createElement('td', '', String(track.notes)), createElement('td', '', formatDuration(track.duration)));
209
+ row.append(track.name ? element('td', '', track.name) : owned('td', '', () => t('media.midi.trackFallback', { index: track.index })), track.instrument ? element('td', '', track.instrument) : owned('td', '', () => t('media.midi.unknownInstrument')), owned('td', '', () => t.formatNumber(track.channel + 1)), owned('td', '', () => t.formatNumber(track.notes)), element('td', '', formatDuration(track.duration)));
170
210
  tbody.append(row);
171
211
  }
172
212
  table.append(thead, tbody);
@@ -174,56 +214,80 @@ const renderMidiElement = (buffer, target, context) => {
174
214
  return wrap;
175
215
  };
176
216
  const renderSummary = (input) => {
177
- title.textContent = input.name;
217
+ bindings.release(title);
218
+ if (input.name)
219
+ title.textContent = input.name;
220
+ else
221
+ bindings.text(title, () => t('media.midi.title'));
178
222
  const totalNotes = input.tracks.reduce((sum, track) => sum + track.notes, 0);
179
- const stats = createElement('div', 'fv-midi-stats');
223
+ const stats = element('div', 'fv-midi-stats');
180
224
  for (const [label, value] of [
181
- [t('media.midi.durationStat'), formatDuration(input.duration)],
182
- ['PPQ', String(input.ppq)],
183
- [t('media.midi.trackStat'), String(input.tracks.length)],
184
- [t('media.midi.noteStat'), String(totalNotes)]
225
+ [() => t('media.midi.durationStat'), () => formatDuration(input.duration)],
226
+ [() => 'PPQ', () => t.formatNumber(input.ppq)],
227
+ [() => t('media.midi.trackStat'), () => t.formatNumber(input.tracks.length)],
228
+ [() => t('media.midi.noteStat'), () => t.formatNumber(totalNotes)]
185
229
  ]) {
186
230
  const stat = document.createElement('div');
187
- stat.append(createElement('span', '', label), createElement('strong', '', value));
231
+ stat.append(owned('span', '', label), owned('strong', '', value));
188
232
  stats.append(stat);
189
233
  }
234
+ bindings.release(body);
190
235
  body.replaceWith(stats, renderTrackTable(input.tracks));
191
236
  };
192
237
  void (async () => {
193
238
  try {
194
239
  const loadMidi = getFileViewerMidiLoader();
195
240
  if (!loadMidi) {
196
- throw new Error('MIDI support is opt-in. Run `npx file-viewer-cli add midi --write`, then `npx file-viewer-cli install --yes`.');
241
+ ownedErrorKey = 'media.midi.install';
242
+ throw t.error('media.midi.install');
197
243
  }
198
244
  const { Midi } = await loadMidi();
245
+ if (disposed)
246
+ return;
199
247
  const midi = new Midi(buffer);
200
248
  if (disposed) {
201
249
  return;
202
250
  }
203
- renderSummary({
204
- name: midi.name || t('media.midi.title'),
251
+ const summary = {
252
+ name: midi.name || '',
205
253
  duration: midi.duration,
206
254
  ppq: midi.header.ppq,
207
255
  tracks: midi.tracks.map((track, index) => {
208
256
  var _a, _b;
209
257
  return ({
210
- name: track.name || `Track ${index + 1}`,
211
- instrument: ((_a = track.instrument) === null || _a === void 0 ? void 0 : _a.name) || ((_b = track.instrument) === null || _b === void 0 ? void 0 : _b.family) || 'Unknown',
258
+ index: index + 1,
259
+ name: track.name || '',
260
+ instrument: ((_a = track.instrument) === null || _a === void 0 ? void 0 : _a.name) || ((_b = track.instrument) === null || _b === void 0 ? void 0 : _b.family) || '',
212
261
  channel: track.channel,
213
262
  notes: track.notes.length,
214
263
  duration: track.duration
215
264
  });
216
265
  })
217
- });
266
+ };
267
+ if (disposed)
268
+ return;
269
+ renderSummary(summary);
218
270
  }
219
271
  catch (error) {
220
- if (!disposed) {
221
- renderError(error instanceof Error ? error.message : t('media.midi.parseFailed'));
272
+ if (disposed)
273
+ return;
274
+ let detail = '';
275
+ let ownedFallback = Boolean(ownedErrorKey) || !(error instanceof Error);
276
+ try {
277
+ if (!ownedFallback)
278
+ detail = error.message;
279
+ }
280
+ catch {
281
+ ownedFallback = true;
222
282
  }
283
+ if (disposed)
284
+ return;
285
+ renderError(detail, ownedFallback);
223
286
  }
224
287
  })();
225
288
  return createRenderedInstance(target, () => {
226
289
  disposed = true;
290
+ bindings.destroy();
227
291
  });
228
292
  };
229
293
  export default async function renderAudio(buffer, target, type, context) {
@@ -1,4 +1,4 @@
1
- import { createFileViewerTranslator } from '@file-viewer/core';
1
+ import { createFileViewerTranslator, createFileViewerI18nBindings } from '@file-viewer/core';
2
2
  const createElement = (tagName, className, text) => {
3
3
  const element = document.createElement(tagName);
4
4
  if (className)
@@ -19,42 +19,59 @@ const formatTime = (seconds) => {
19
19
  : `${String(minutes).padStart(2, '0')}:${String(rest).padStart(2, '0')}`;
20
20
  };
21
21
  export const mountMp4vSoftwarePlayer = async (buffer, video, track, context) => {
22
- var _a;
22
+ var _a, _b, _c, _d;
23
+ if ((_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.aborted)
24
+ throw new DOMException('The video request was aborted.', 'AbortError');
23
25
  if (typeof Worker === 'undefined')
24
26
  throw new Error('Web Worker is unavailable.');
25
27
  if (typeof HTMLCanvasElement.prototype.transferControlToOffscreen !== 'function') {
26
28
  throw new Error('OffscreenCanvas is unavailable.');
27
29
  }
28
- const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
29
- const player = createElement('div', 'fv-video-software-player');
30
+ const document = video.ownerDocument;
31
+ const bindings = createFileViewerI18nBindings(context === null || context === void 0 ? void 0 : context.i18n);
32
+ const element = (tag, className, text) => {
33
+ const node = document.createElement(tag);
34
+ if (className)
35
+ node.className = className;
36
+ if (text !== undefined)
37
+ node.textContent = text;
38
+ return node;
39
+ };
40
+ const owned = (tag, className, value) => {
41
+ const node = element(tag, className);
42
+ bindings.text(node, value);
43
+ return node;
44
+ };
45
+ const t = createFileViewerTranslator((context === null || context === void 0 ? void 0 : context.i18n) || (context === null || context === void 0 ? void 0 : context.options));
46
+ const player = element('div', 'fv-video-software-player');
30
47
  player.tabIndex = 0;
31
48
  player.dataset.state = 'loading';
32
49
  player.style.aspectRatio = `${track.width} / ${track.height}`;
33
- const canvas = createElement('canvas', 'fv-video-software-canvas');
50
+ const canvas = element('canvas', 'fv-video-software-canvas');
34
51
  canvas.width = track.width;
35
52
  canvas.height = track.height;
36
- const loading = createElement('div', 'fv-video-software-loading', t('media.video.softwareLoading'));
53
+ const loading = owned('div', 'fv-video-software-loading', () => t('media.video.softwareLoading'));
37
54
  loading.setAttribute('role', 'status');
38
- const badge = createElement('span', 'fv-video-software-badge', t('media.video.softwareBadge'));
39
- const controls = createElement('div', 'fv-video-software-controls');
40
- const playButton = createElement('button', 'fv-video-software-button', '▶');
55
+ const badge = owned('span', 'fv-video-software-badge', () => t('media.video.softwareBadge'));
56
+ const controls = element('div', 'fv-video-software-controls');
57
+ const playButton = element('button', 'fv-video-software-button', '▶');
41
58
  playButton.type = 'button';
42
- playButton.setAttribute('aria-label', t('media.video.play'));
43
- const currentTime = createElement('span', 'fv-video-software-time', '00:00');
44
- const seek = createElement('input', 'fv-video-software-seek');
59
+ bindings.attr(playButton, 'aria-label', () => t(video.paused ? 'media.video.play' : 'media.video.pause'));
60
+ const currentTime = element('span', 'fv-video-software-time', '00:00');
61
+ const seek = element('input', 'fv-video-software-seek');
45
62
  seek.type = 'range';
46
63
  seek.min = '0';
47
64
  seek.max = String(track.duration / track.timescale);
48
65
  seek.step = '0.01';
49
66
  seek.value = '0';
50
- seek.setAttribute('aria-label', t('media.video.seek'));
51
- const duration = createElement('span', 'fv-video-software-time', formatTime(track.duration / track.timescale));
52
- const volumeButton = createElement('button', 'fv-video-software-button', '🔊');
67
+ bindings.attr(seek, 'aria-label', () => t('media.video.seek'));
68
+ const duration = element('span', 'fv-video-software-time', formatTime(track.duration / track.timescale));
69
+ const volumeButton = element('button', 'fv-video-software-button', '🔊');
53
70
  volumeButton.type = 'button';
54
- volumeButton.setAttribute('aria-label', t('media.video.mute'));
55
- const fullscreenButton = createElement('button', 'fv-video-software-button', '⛶');
71
+ bindings.attr(volumeButton, 'aria-label', () => t(video.muted || video.volume === 0 ? 'media.video.unmute' : 'media.video.mute'));
72
+ const fullscreenButton = element('button', 'fv-video-software-button', '⛶');
56
73
  fullscreenButton.type = 'button';
57
- fullscreenButton.setAttribute('aria-label', t('media.video.fullscreen'));
74
+ bindings.attr(fullscreenButton, 'aria-label', () => t('media.video.fullscreen'));
58
75
  controls.append(playButton, currentTime, seek, duration, volumeButton, fullscreenButton);
59
76
  video.controls = false;
60
77
  video.className = 'fv-video-audio-clock';
@@ -78,6 +95,8 @@ export const mountMp4vSoftwarePlayer = async (buffer, video, track, context) =>
78
95
  worker.postMessage({ type, time: video.currentTime || 0 });
79
96
  };
80
97
  const syncControls = () => {
98
+ if (disposed)
99
+ return;
81
100
  const value = Number.isFinite(video.currentTime) ? video.currentTime : 0;
82
101
  currentTime.textContent = formatTime(value);
83
102
  seek.value = String(value);
@@ -105,10 +124,24 @@ export const mountMp4vSoftwarePlayer = async (buffer, video, track, context) =>
105
124
  const onVolumeChange = () => syncControls();
106
125
  const onAbort = () => cleanup();
107
126
  const togglePlayback = () => {
127
+ if (disposed)
128
+ return;
108
129
  if (video.paused) {
109
130
  void video.play().catch((error) => {
131
+ if (disposed)
132
+ return;
133
+ let detail;
134
+ try {
135
+ detail = error instanceof Error ? error.message : String(error);
136
+ }
137
+ catch {
138
+ return;
139
+ }
140
+ if (disposed)
141
+ return;
110
142
  player.dataset.state = 'error';
111
- loading.textContent = error instanceof Error ? error.message : String(error);
143
+ bindings.release(loading);
144
+ loading.textContent = detail;
112
145
  });
113
146
  }
114
147
  else {
@@ -149,6 +182,9 @@ export const mountMp4vSoftwarePlayer = async (buffer, video, track, context) =>
149
182
  if (disposed)
150
183
  return;
151
184
  disposed = true;
185
+ if (!ready)
186
+ rejectReady === null || rejectReady === void 0 ? void 0 : rejectReady(new DOMException('The video request was aborted.', 'AbortError'));
187
+ bindings.destroy();
152
188
  video.pause();
153
189
  worker.postMessage({ type: 'dispose' });
154
190
  worker.terminate();
@@ -167,6 +203,8 @@ export const mountMp4vSoftwarePlayer = async (buffer, video, track, context) =>
167
203
  (_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener('abort', onAbort);
168
204
  };
169
205
  worker.onmessage = (event) => {
206
+ if (disposed)
207
+ return;
170
208
  const message = event.data;
171
209
  if (message.type === 'ready') {
172
210
  ready = true;
@@ -184,18 +222,35 @@ export const mountMp4vSoftwarePlayer = async (buffer, video, track, context) =>
184
222
  player.dataset.decodeMs = String(message.decodeMs);
185
223
  return;
186
224
  }
187
- const error = new Error(message.message);
225
+ const detail = message.message;
226
+ if (disposed)
227
+ return;
228
+ const error = new Error(detail);
188
229
  player.dataset.state = 'error';
189
230
  loading.hidden = false;
190
- loading.textContent = message.message;
231
+ bindings.release(loading);
232
+ loading.textContent = detail;
191
233
  if (!ready)
192
234
  rejectReady === null || rejectReady === void 0 ? void 0 : rejectReady(error);
193
235
  };
194
236
  worker.onerror = (event) => {
195
- const error = new Error(event.message || 'The MP4V decoder Worker failed.');
237
+ if (disposed)
238
+ return;
239
+ const detail = event.message;
240
+ if (disposed)
241
+ return;
242
+ const error = new Error(detail || t('media.video.softwareWorkerFailed'));
243
+ if (disposed)
244
+ return;
196
245
  player.dataset.state = 'error';
197
246
  loading.hidden = false;
198
- loading.textContent = error.message;
247
+ if (detail) {
248
+ bindings.release(loading);
249
+ loading.textContent = detail;
250
+ }
251
+ else {
252
+ bindings.text(loading, () => t('media.video.softwareWorkerFailed'));
253
+ }
199
254
  if (!ready)
200
255
  rejectReady === null || rejectReady === void 0 ? void 0 : rejectReady(error);
201
256
  };
@@ -211,14 +266,22 @@ export const mountMp4vSoftwarePlayer = async (buffer, video, track, context) =>
211
266
  volumeButton.addEventListener('click', onVolumeClick);
212
267
  fullscreenButton.addEventListener('click', onFullscreenClick);
213
268
  player.addEventListener('keydown', onPlayerKeydown);
214
- (_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', onAbort, { once: true });
269
+ (_b = context === null || context === void 0 ? void 0 : context.signal) === null || _b === void 0 ? void 0 : _b.addEventListener('abort', onAbort, { once: true });
215
270
  syncControls();
216
- const offscreen = canvas.transferControlToOffscreen();
217
- worker.postMessage({ type: 'init', buffer, canvas: offscreen }, [buffer, offscreen]);
218
271
  try {
272
+ if ((_c = context === null || context === void 0 ? void 0 : context.signal) === null || _c === void 0 ? void 0 : _c.aborted)
273
+ cleanup();
274
+ if (!disposed) {
275
+ const offscreen = canvas.transferControlToOffscreen();
276
+ worker.postMessage({ type: 'init', buffer, canvas: offscreen }, [buffer, offscreen]);
277
+ }
219
278
  await readyPromise;
279
+ if (disposed || ((_d = context === null || context === void 0 ? void 0 : context.signal) === null || _d === void 0 ? void 0 : _d.aborted)) {
280
+ throw new DOMException('The video request was aborted.', 'AbortError');
281
+ }
220
282
  }
221
283
  catch (error) {
284
+ void readyPromise.catch(() => { });
222
285
  cleanup();
223
286
  throw error;
224
287
  }
package/dist/video.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createFileViewerTranslator } from '@file-viewer/core';
1
+ import { createFileViewerTranslator, createFileViewerI18nBindings } from '@file-viewer/core';
2
2
  import { getFileViewerHlsLoader } from './optionalCapabilities.js';
3
3
  import { extractMp4vSoftwareTrack, inspectMp4VideoTrack } from './mp4.js';
4
4
  import { mountMp4vSoftwarePlayer } from './mp4vPlayer.js';
@@ -80,17 +80,33 @@ class UnsupportedVideoCodecError extends Error {
80
80
  this.track = track;
81
81
  }
82
82
  }
83
- const createCompatibilityNotice = (track, t) => {
84
- const notice = createElement('div', 'fv-video-compatibility');
83
+ const createCompatibilityNotice = (track, t, element, owned) => {
84
+ const notice = element('div', 'fv-video-compatibility');
85
85
  notice.setAttribute('role', 'alert');
86
86
  notice.dataset.codec = track.codec;
87
- const card = createElement('div', 'fv-video-compatibility-card');
88
- card.append(createElement('span', '', t('media.video.codecUnsupportedBadge')), createElement('strong', '', t('media.video.codecUnsupportedTitle')), createElement('p', '', t('media.video.codecUnsupportedDescription', { codec: track.label })), createElement('code', '', track.contentType), createElement('p', '', t('media.video.codecUnsupportedAction')));
87
+ const card = element('div', 'fv-video-compatibility-card');
88
+ card.append(owned('span', '', () => t('media.video.codecUnsupportedBadge')), owned('strong', '', () => t('media.video.codecUnsupportedTitle')), owned('p', '', () => t('media.video.codecUnsupportedDescription', { codec: track.label })), element('code', '', track.contentType), owned('p', '', () => t('media.video.codecUnsupportedAction')));
89
89
  notice.append(card);
90
90
  return notice;
91
91
  };
92
92
  export default async function renderVideo(buffer, target, type, context) {
93
- const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
93
+ var _a, _b;
94
+ const document = target.ownerDocument;
95
+ const bindings = createFileViewerI18nBindings(context === null || context === void 0 ? void 0 : context.i18n);
96
+ const element = (tag, className, text) => {
97
+ const node = document.createElement(tag);
98
+ if (className)
99
+ node.className = className;
100
+ if (text !== undefined)
101
+ node.textContent = text;
102
+ return node;
103
+ };
104
+ const owned = (tag, className, value) => {
105
+ const node = element(tag, className);
106
+ bindings.text(node, value);
107
+ return node;
108
+ };
109
+ const t = createFileViewerTranslator((context === null || context === void 0 ? void 0 : context.i18n) || (context === null || context === void 0 ? void 0 : context.options));
94
110
  const normalizedType = (type || 'mp4').toLowerCase();
95
111
  const mp4VideoTrack = normalizedType === 'mp4'
96
112
  ? inspectMp4VideoTrack(buffer)
@@ -102,20 +118,22 @@ export default async function renderVideo(buffer, target, type, context) {
102
118
  let objectUrl = '';
103
119
  let hls = null;
104
120
  let softwarePlayer = null;
105
- const root = createElement('div', 'fv-video-viewer');
106
- const shell = createElement('section', 'fv-video-shell');
107
- const heading = createElement('div', 'fv-video-heading');
108
- heading.append(createElement('span', '', normalizedType.toUpperCase() || 'VIDEO'), createElement('strong', '', t('media.video.title')));
109
- const video = createElement('video', 'fv-video-player');
121
+ const root = element('div', 'fv-video-viewer');
122
+ const shell = element('section', 'fv-video-shell');
123
+ const heading = element('div', 'fv-video-heading');
124
+ heading.append(element('span', '', normalizedType.toUpperCase() || 'VIDEO'), owned('strong', '', () => t('media.video.title')));
125
+ const video = element('video', 'fv-video-player');
110
126
  video.controls = true;
111
127
  video.preload = 'metadata';
112
- video.textContent = t('media.video.unsupported');
128
+ bindings.text(video, () => t('media.video.unsupported'));
113
129
  shell.append(heading, video);
114
130
  if (normalizedType === 'm3u8') {
115
- shell.append(createElement('p', 'fv-video-hint', t('media.video.hlsHint')));
131
+ shell.append(owned('p', 'fv-video-hint', () => t('media.video.hlsHint')));
116
132
  }
117
133
  root.append(shell);
118
- target.replaceChildren(createStyle(), root);
134
+ const style = document.createElement('style');
135
+ style.textContent = videoStyle;
136
+ target.replaceChildren(style, root);
119
137
  const resolveSource = () => {
120
138
  if (normalizedType === 'm3u8' && (context === null || context === void 0 ? void 0 : context.url)) {
121
139
  return context.url;
@@ -204,7 +222,10 @@ export default async function renderVideo(buffer, target, type, context) {
204
222
  }
205
223
  };
206
224
  const cleanup = () => {
225
+ if (disposed)
226
+ return;
207
227
  disposed = true;
228
+ bindings.destroy();
208
229
  softwarePlayer === null || softwarePlayer === void 0 ? void 0 : softwarePlayer.cleanup();
209
230
  softwarePlayer = null;
210
231
  releasePlaybackResources();
@@ -218,9 +239,18 @@ export default async function renderVideo(buffer, target, type, context) {
218
239
  if (error.track.codec === 'mp4v' && mp4vSoftwareTrack) {
219
240
  try {
220
241
  softwarePlayer = await mountMp4vSoftwarePlayer(buffer, video, mp4vSoftwareTrack, context);
242
+ if ((_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
243
+ throw new DOMException('The video request was aborted.', 'AbortError');
244
+ }
221
245
  root.dataset.videoState = 'software-decoder';
222
246
  }
223
247
  catch (softwareError) {
248
+ if ((_b = context === null || context === void 0 ? void 0 : context.signal) === null || _b === void 0 ? void 0 : _b.aborted) {
249
+ cleanup();
250
+ root.remove();
251
+ style.remove();
252
+ throw softwareError;
253
+ }
224
254
  releasePlaybackResources();
225
255
  root.dataset.videoState = 'unsupported-codec';
226
256
  root.dataset.videoError = softwareError instanceof Error
@@ -228,13 +258,15 @@ export default async function renderVideo(buffer, target, type, context) {
228
258
  : String(softwareError);
229
259
  const failedPlayer = shell.querySelector('.fv-video-software-player');
230
260
  const failedElement = failedPlayer || video;
231
- failedElement.replaceWith(createCompatibilityNotice(error.track, t));
261
+ bindings.release(video);
262
+ failedElement.replaceWith(createCompatibilityNotice(error.track, t, element, owned));
232
263
  }
233
264
  }
234
265
  else {
235
266
  releasePlaybackResources();
236
267
  root.dataset.videoState = 'unsupported-codec';
237
- video.replaceWith(createCompatibilityNotice(error.track, t));
268
+ bindings.release(video);
269
+ video.replaceWith(createCompatibilityNotice(error.track, t, element, owned));
238
270
  }
239
271
  }
240
272
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/renderer-media",
3
- "version": "3.3.3",
3
+ "version": "3.3.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Standalone native audio and video renderer for File Viewer; HLS fallback and MIDI inspection are explicit opt-in capabilities.",
@@ -75,7 +75,7 @@
75
75
  "README.zh-CN.md"
76
76
  ],
77
77
  "dependencies": {
78
- "@file-viewer/core": "3.3.3"
78
+ "@file-viewer/core": "3.3.5"
79
79
  },
80
80
  "fileViewer": {
81
81
  "capabilityManifest": "./file-viewer.capability.json",