Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 100x 100x 14x 18x 18x 18x 17x 16x 16x 2x 14x 72x 14x 5x 9x 1x 8x 6x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 5x 5x 5x 5x 5x 5x 15x 15x 15x 15x | /*
Copyright (C) 2026 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import type { CatalogPluginMetadata } from '../plugin-manager';
import { parsePluginCatalog } from '../parse-plugin-metadata';
const PLUGIN_METADATA_FIELDS = [
'id',
'version',
'apiVersion',
'documents',
'entry',
'styles'
] as const;
export const loadIdeaPlugin = async(
plugin: CatalogPluginMetadata,
isCurrent: () => boolean = () => true
): Promise<void> => {
const elements: HTMLElement[] = [];
try {
const response = await window.$PAPI.getDocumentPluginBundle(plugin.id);
// Пока IDE читала бандл, пользователь мог удалить плагин из каталога.
if (!isCurrent()) throw new Error('Плагин удалён из текущего каталога');
const bundle = response?.data;
const [metadata] = parsePluginCatalog(
{schemaVersion: 1, plugins: [bundle?.metadata]},
plugin.apiVersion,
(message) => { throw new Error(message); }
);
// IDE перечитывает каталог при каждом запросе: не запускаем другой бандл под старой metadata.
const metadataChanged = PLUGIN_METADATA_FIELDS.some(
(field) => JSON.stringify(metadata[field]) !== JSON.stringify(plugin[field])
);
if (metadataChanged) {
throw new Error('Каталог плагинов изменился. Откройте «⋮ → Обновить плагины».');
}
if (metadata.scripts.length) {
throw new Error('Дополнительные JS chunks пока не поддерживаются в IDE');
}
if (typeof bundle.script !== 'string'
|| !Array.isArray(bundle.styles)
|| !bundle.styles.every((style: unknown) => typeof style === 'string')
|| bundle.styles.length !== metadata.styles.length) {
throw new Error('Некорректное содержимое бандла');
}
for (const content of bundle.styles) {
const style = document.createElement('style');
style.textContent = content;
elements.push(style);
document.head.appendChild(style);
}
const script = document.createElement('script');
script.textContent = bundle.script;
elements.push(script);
let executionError: Error | undefined;
const onError = (event: ErrorEvent) => {
executionError = event.error instanceof Error ? event.error : new Error(event.message);
};
script.onerror = () => {
executionError = new Error('Браузер не смог выполнить JS плагина');
};
// Inline script выполняется синхронно, но его исключения приходят через window.error.
window.addEventListener('error', onError);
try {
document.head.appendChild(script);
} finally {
window.removeEventListener('error', onError);
}
if (executionError) throw executionError;
} catch (error: unknown) {
elements.forEach((element) => element.remove());
const bridgeMessage = (error as {response?: {data?: unknown}})?.response?.data;
const message = error instanceof Error
? error.message
: typeof bridgeMessage === 'string'
? bridgeMessage
: 'Неизвестная ошибка IDE';
throw new Error(`Не удалось загрузить плагин [${plugin.id}]: ${message}`);
}
};
|