|
1 |
| -import { BlockNoteEditor, insertOrUpdateBlock } from '@blocknote/core'; |
2 | 1 | import {
|
3 | 2 | BlockFromConfig,
|
| 3 | + BlockNoteEditor, |
4 | 4 | createBlockSpec,
|
5 | 5 | Props,
|
6 | 6 | PropSchema,
|
7 | 7 | } from '@blocknote/core';
|
8 |
| -import { schema } from '@angular-devkit/core'; |
| 8 | +import { |
| 9 | + lucideAlertTriangle, |
| 10 | + lucideCheckCircle, |
| 11 | + lucideCircleOff, |
| 12 | + lucideInfo, |
| 13 | +} from '@ng-icons/lucide'; |
| 14 | + |
| 15 | +// The types of alerts that users can choose from. |
| 16 | +export const alertTypes = [ |
| 17 | + { |
| 18 | + title: 'Warning', |
| 19 | + value: 'warning', |
| 20 | + icon: lucideAlertTriangle, |
| 21 | + color: '#e69819', |
| 22 | + backgroundColor: { |
| 23 | + light: '#fff6e6', |
| 24 | + dark: '#805d20', |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + title: 'Error', |
| 29 | + value: 'error', |
| 30 | + icon: lucideCircleOff, |
| 31 | + color: '#d80d0d', |
| 32 | + backgroundColor: { |
| 33 | + light: '#ffe6e6', |
| 34 | + dark: '#802020', |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + title: 'Info', |
| 39 | + value: 'info', |
| 40 | + icon: lucideInfo, |
| 41 | + color: '#507aff', |
| 42 | + backgroundColor: { |
| 43 | + light: '#e6ebff', |
| 44 | + dark: '#203380', |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + title: 'Success', |
| 49 | + value: 'success', |
| 50 | + icon: lucideCheckCircle, |
| 51 | + color: '#0bc10b', |
| 52 | + backgroundColor: { |
| 53 | + light: '#e6ffe6', |
| 54 | + dark: '#208020', |
| 55 | + }, |
| 56 | + }, |
| 57 | +] as const; |
9 | 58 |
|
10 |
| -export const alertPropSchema = {} satisfies PropSchema; |
| 59 | +export const alertPropSchema = { |
| 60 | + type: { |
| 61 | + default: 'warning', |
| 62 | + values: alertTypes.map((type) => type.value), |
| 63 | + }, |
| 64 | +} satisfies PropSchema; |
11 | 65 |
|
12 | 66 | export const alertBlockConfig = {
|
13 | 67 | type: 'alert' as const,
|
14 | 68 | propSchema: alertPropSchema,
|
15 |
| - content: 'none', |
| 69 | + content: 'inline', |
16 | 70 | } as const;
|
17 | 71 |
|
18 | 72 | const alertRender = (
|
19 | 73 | block: BlockFromConfig<typeof alertBlockConfig, any, any>,
|
20 | 74 | editor: BlockNoteEditor<any, any, any>
|
21 | 75 | ) => {
|
22 |
| - const div = document.createElement('p'); |
23 |
| - div.textContent = 'Alert'; |
| 76 | + const div = document.createElement('div'); |
| 77 | + div.style.width = '100%'; |
| 78 | + div.style.padding = '4px 8px'; |
| 79 | + div.style.display = 'flex'; |
| 80 | + div.style.alignItems = 'center'; |
| 81 | + const alertType = alertTypes.find((type) => type.value === block.props.type); |
| 82 | + console.log(block, alertType); |
| 83 | + if (!alertType) { |
| 84 | + return { |
| 85 | + dom: div, |
| 86 | + }; |
| 87 | + } |
| 88 | + div.style.color = alertType.color; |
| 89 | + div.style.backgroundColor = alertType.backgroundColor.light; |
| 90 | + |
| 91 | + // Create a select element. |
| 92 | + const selectElement = document.createElement('select'); |
| 93 | + |
| 94 | + // Populate the select element with options. |
| 95 | + alertTypes.forEach((alertType) => { |
| 96 | + const option = document.createElement('option'); |
| 97 | + |
| 98 | + option.value = alertType.value; |
| 99 | + option.text = alertType.title; |
| 100 | + |
| 101 | + // Mark the current alertType as selected in the dropdown. |
| 102 | + if (alertType.value === block.props.type) { |
| 103 | + option.selected = true; |
| 104 | + } |
| 105 | + |
| 106 | + selectElement.appendChild(option); |
| 107 | + }); |
| 108 | + |
| 109 | + // Handle the change event. |
| 110 | + selectElement.addEventListener('change', (event: Event) => { |
| 111 | + const target = event.target as HTMLSelectElement; |
| 112 | + editor.updateBlock(block, { |
| 113 | + type: 'alert', |
| 114 | + props: { type: target.value }, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + // Style the dropdown |
| 119 | + selectElement.style.position = 'absolute'; |
| 120 | + selectElement.style.right = '100px'; |
| 121 | + |
| 122 | + // Add the select element to the div. |
| 123 | + div.appendChild(selectElement); |
| 124 | + |
| 125 | + const iconElement = document.createElement('span'); |
| 126 | + iconElement.innerHTML = alertType.icon; |
| 127 | + iconElement.style.marginRight = '8px'; |
| 128 | + div.appendChild(iconElement); |
| 129 | + |
| 130 | + const textElement = document.createElement('span'); |
| 131 | + textElement.classList.add('inline-content'); |
| 132 | + textElement.textContent = 'Hello world'; |
| 133 | + div.appendChild(textElement); |
24 | 134 |
|
25 | 135 | return {
|
26 | 136 | dom: div,
|
|
0 commit comments