( editor: SlateEditor, plugins: SlatePlugins )
| 491 | }; |
| 492 | |
| 493 | export const resolveAndSortPlugins = ( |
| 494 | editor: SlateEditor, |
| 495 | plugins: SlatePlugins |
| 496 | ): SlatePlugins => { |
| 497 | // Step 1: Resolve, flatten, and merge all plugins |
| 498 | const pluginMap = flattenAndResolvePlugins(editor, plugins); |
| 499 | |
| 500 | // Step 2: Filter out disabled plugins |
| 501 | const enabledPlugins = Array.from(pluginMap.values()).filter( |
| 502 | (plugin) => plugin.enabled !== false |
| 503 | ); |
| 504 | |
| 505 | // Step 3: Sort plugins by priority |
| 506 | enabledPlugins.sort((a, b) => b.priority - a.priority); |
| 507 | |
| 508 | // Step 4: Reorder based on dependencies |
| 509 | const orderedPlugins: SlatePlugins = []; |
| 510 | const visited = new Set<string>(); |
| 511 | |
| 512 | const visit = (plugin: SlatePlugin) => { |
| 513 | if (visited.has(plugin.key)) return; |
| 514 | |
| 515 | visited.add(plugin.key); |
| 516 | |
| 517 | plugin.dependencies?.forEach((depKey) => { |
| 518 | const depPlugin = pluginMap.get(depKey); |
| 519 | |
| 520 | if (depPlugin) { |
| 521 | visit(depPlugin); |
| 522 | } else { |
| 523 | editor.api.debug.warn( |
| 524 | `Plugin "${plugin.key}" depends on missing plugin "${depKey}"`, |
| 525 | 'PLUGIN_DEPENDENCY_MISSING' |
| 526 | ); |
| 527 | } |
| 528 | }); |
| 529 | |
| 530 | orderedPlugins.push(plugin); |
| 531 | }; |
| 532 | |
| 533 | enabledPlugins.forEach(visit); |
| 534 | |
| 535 | return orderedPlugins; |
| 536 | }; |
| 537 | |
| 538 | export const applyPluginsToEditor = ( |
| 539 | editor: SlateEditor, |
searching dependent graphs…