Skip to content

Commit de337a7

Browse files
committed
feat: alert now shows icon and style depending from alert type
1 parent 89364cf commit de337a7

File tree

2 files changed

+142
-27
lines changed

2 files changed

+142
-27
lines changed

docs/src/pages/examples/custom/alert-block/alert-block.example.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { CommonModule } from '@angular/common';
22
import { Component } from '@angular/core';
33
import {
4-
BlockConfig,
54
BlockNoteEditor,
65
BlockSpecs,
7-
InlineContentSchema,
8-
StyleSchema,
96
defaultBlockSpecs,
10-
insertOrUpdateBlock, DefaultSuggestionItem
7+
DefaultSuggestionItem,
8+
insertOrUpdateBlock,
119
} from '@blocknote/core';
1210
import { BnaEditorComponent } from '@dytab/block-note-angular';
1311
import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
@@ -21,40 +19,47 @@ import { alertBlock } from './alert-block';
2119
[initialContent]="initialContent"
2220
[blockSpecs]="blockSpecs"
2321
[inputSlashMenuItems]="suggestionItem"
24-
/>`
22+
/>`,
2523
})
26-
export class AlertBlockExample{
24+
export class AlertBlockExample {
2725
initialContent = [
2826
{
29-
type: 'alert'
30-
}
27+
type: 'alert',
28+
props: {
29+
type: 'warning',
30+
},
31+
},
3132
//TODO: remove cast
3233
] as any;
3334
blockSpecs: BlockSpecs = {
3435
...defaultBlockSpecs,
35-
alert: alertBlock
36+
alert: alertBlock,
3637
};
37-
insertAlert = (editor: BlockNoteEditor): Omit<DefaultSuggestionItem, 'key'> => ({
38-
title: "Alert",
38+
insertAlert = (
39+
editor: BlockNoteEditor
40+
): Omit<DefaultSuggestionItem, 'key'> => ({
41+
title: 'Alert',
3942
onItemClick: () => {
4043
insertOrUpdateBlock(editor, {
41-
type: "alert" as never,
44+
type: 'alert' as never,
4245
});
4346
},
4447
badge: 'BAFD',
4548
subtext: 'SUBTEXT',
4649
aliases: [
47-
"alert",
48-
"notification",
49-
"emphasize",
50-
"warning",
51-
"error",
52-
"info",
53-
"success",
50+
'alert',
51+
'notification',
52+
'emphasize',
53+
'warning',
54+
'error',
55+
'info',
56+
'success',
5457
],
55-
group: "Other",
58+
group: 'Other',
5659
});
57-
suggestionItem: Array<(editor: BlockNoteEditor) => Omit<DefaultSuggestionItem, 'key'>> = [this.insertAlert]
60+
suggestionItem: Array<
61+
(editor: BlockNoteEditor) => Omit<DefaultSuggestionItem, 'key'>
62+
> = [this.insertAlert];
5863
}
5964

6065
export const alertBlockExampleCode = `import { CommonModule } from '@angular/common';

docs/src/pages/examples/custom/alert-block/alert-block.ts

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,136 @@
1-
import { BlockNoteEditor, insertOrUpdateBlock } from '@blocknote/core';
21
import {
32
BlockFromConfig,
3+
BlockNoteEditor,
44
createBlockSpec,
55
Props,
66
PropSchema,
77
} 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;
958

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;
1165

1266
export const alertBlockConfig = {
1367
type: 'alert' as const,
1468
propSchema: alertPropSchema,
15-
content: 'none',
69+
content: 'inline',
1670
} as const;
1771

1872
const alertRender = (
1973
block: BlockFromConfig<typeof alertBlockConfig, any, any>,
2074
editor: BlockNoteEditor<any, any, any>
2175
) => {
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);
24134

25135
return {
26136
dom: div,

0 commit comments

Comments
 (0)