Skip to content

Commit c35b0f4

Browse files
committed
feat: add inputs for editor specs
1 parent 1d7516a commit c35b0f4

10 files changed

+98
-103
lines changed

src/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<bna-test-editor/>
2-
<bna-custom-block-example></bna-custom-block-example>
1+
<!--<bna-test-editor/>-->
2+
<bna-custom-alert-block-example></bna-custom-alert-block-example>

src/app/app.component.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { Component } from '@angular/core';
22
import { RouterModule } from '@angular/router';
3+
import { CustomAlertBlockExampleComponent } from '../examples/custom-alert-block/custom-alert-block-example.component';
34
import { TestHostBlockNodeEditorComponent } from '../ui/block-note-editor/_usage/test-host.component';
45
import { NxWelcomeComponent } from './nx-welcome.component';
5-
import {
6-
CustomBlockExampleComponent
7-
} from '../examples/custom-blocks/custom-block-example.component';
86

97
@Component({
108
standalone: true,
11-
imports: [NxWelcomeComponent, RouterModule, TestHostBlockNodeEditorComponent, CustomBlockExampleComponent],
9+
imports: [
10+
NxWelcomeComponent,
11+
RouterModule,
12+
TestHostBlockNodeEditorComponent,
13+
CustomAlertBlockExampleComponent,
14+
],
1215
selector: 'app-root',
1316
templateUrl: './app.component.html',
1417
styleUrl: './app.component.scss',

src/examples/alert/alert-block.ts renamed to src/examples/custom-alert-block/alert-block.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const alertRender = (
1919
editor: BlockNoteEditor<any, any, any>
2020
) => {
2121
const div = document.createElement('p');
22-
div.textContent = 'ALER asdasdT';
22+
div.textContent = 'Alert';
2323

2424
return {
2525
dom: div,

src/examples/custom-blocks/custom-block-example.component.html renamed to src/examples/custom-alert-block/custom-alert-block-example.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<bna-editor
99
formControlName="editor"
1010
class="block min-h-52 bg-gray-200 rounded"
11+
[blockSpecs]="blockSpecs"
1112

1213
/>
1314
@if (control.invalid && control.touched) {

src/examples/custom-blocks/custom-block-example.component.spec.ts renamed to src/examples/custom-alert-block/custom-alert-block-example.component.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { CustomBlockExampleComponent } from './custom-block-example.component';
2+
import { CustomAlertBlockExampleComponent } from './custom-alert-block-example.component';
33

44
describe('CustomBlockExampleComponent', () => {
5-
let component: CustomBlockExampleComponent;
6-
let fixture: ComponentFixture<CustomBlockExampleComponent>;
5+
let component: CustomAlertBlockExampleComponent;
6+
let fixture: ComponentFixture<CustomAlertBlockExampleComponent>;
77

88
beforeEach(async () => {
99
await TestBed.configureTestingModule({
10-
imports: [CustomBlockExampleComponent],
10+
imports: [CustomAlertBlockExampleComponent],
1111
}).compileComponents();
1212

13-
fixture = TestBed.createComponent(CustomBlockExampleComponent);
13+
fixture = TestBed.createComponent(CustomAlertBlockExampleComponent);
1414
component = fixture.componentInstance;
1515
fixture.detectChanges();
1616
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { CommonModule } from '@angular/common';
2+
import { Component } from '@angular/core';
3+
import {
4+
NonNullableFormBuilder,
5+
ReactiveFormsModule,
6+
Validators,
7+
} from '@angular/forms';
8+
import { Block, BlockSpecs, defaultBlockSpecs } from '@blocknote/core';
9+
import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
10+
import { BnaEditorComponent } from '../../ui/block-note-editor/_component/bna-editor.component';
11+
import { alertBlock } from './alert-block';
12+
13+
@Component({
14+
selector: 'bna-custom-alert-block-example',
15+
standalone: true,
16+
imports: [
17+
CommonModule,
18+
ReactiveFormsModule,
19+
BnaEditorComponent,
20+
HlmButtonDirective,
21+
],
22+
templateUrl: './custom-alert-block-example.component.html',
23+
styleUrl: './custom-alert-block-example.component.css',
24+
})
25+
export class CustomAlertBlockExampleComponent {
26+
constructor(private formBuilder: NonNullableFormBuilder) {}
27+
28+
form = this.formBuilder.group({
29+
editor: this.formBuilder.control<Block[] | undefined>(
30+
[
31+
{
32+
id: '262bf9c4-3c2a-4543-bc47-49976ec904c3',
33+
type: 'alert',
34+
props: {},
35+
content: [],
36+
children: [],
37+
},
38+
//TODO: remove cast
39+
] as any,
40+
Validators.required
41+
),
42+
});
43+
blockSpecs: BlockSpecs = {
44+
...defaultBlockSpecs,
45+
alert: alertBlock,
46+
};
47+
48+
get control() {
49+
return this.form.controls.editor;
50+
}
51+
52+
submit() {
53+
this.form.markAllAsTouched();
54+
if (!this.form.valid) return;
55+
56+
console.log('submitted', this.form.value);
57+
}
58+
}

src/examples/custom-blocks/custom-block-example.component.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/examples/custom-blocks/custom-block.ts

Whitespace-only changes.

src/ui/block-note-editor/_component/bna-editor.component.ts

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ import {
1717
Block,
1818
BlockNoteEditor,
1919
BlockNoteSchema,
20+
BlockSpecs,
2021
defaultBlockSpecs,
2122
defaultInlineContentSpecs,
2223
defaultStyleSpecs,
2324
DefaultSuggestionItem,
2425
getDefaultSlashMenuItems,
25-
insertOrUpdateBlock,
26-
PartialBlock,
26+
InlineContentSpecs,
27+
StyleSpecs,
2728
} from '@blocknote/core';
2829
import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
2930
import { HlmCardDirective } from '@spartan-ng/ui-card-helm';
@@ -36,7 +37,6 @@ import {
3637
HlmMenuSeparatorComponent,
3738
HlmMenuShortcutComponent,
3839
} from '@spartan-ng/ui-menu-helm';
39-
import { alertBlock } from '../../../examples/alert/alert-block';
4040
import { BnaFormattingToolbarDirective } from '../../components/bna-formatting-toolbar/bna-formatting-toolbar.directive';
4141
import { BnaSideMenuDirective } from '../../components/bna-side-menu/bna-side-menu.directive';
4242
import { BnaAddBlockButtonComponent } from '../../components/bna-side-menu/default-buttons/add-block-button/bna-add-block-button.component';
@@ -84,7 +84,10 @@ export class BnaEditorComponent implements ControlValueAccessor {
8484
formControl = input<FormControl>();
8585
formControlName = input<string>();
8686
labelForId = input<string>();
87-
//TODO: add input for blocks, ...
87+
blockSpecs = input<BlockSpecs>();
88+
inlineContentSpecs = input<InlineContentSpecs>();
89+
styleSpecs = input<StyleSpecs>();
90+
inputSlashMenuItems = input<DefaultSuggestionItem[]>();
8891

8992
isDisabled = false;
9093

@@ -93,8 +96,6 @@ export class BnaEditorComponent implements ControlValueAccessor {
9396

9497
addedElement = 0;
9598

96-
// formattingToolbar!: FormattingToolbar;
97-
9899
constructor(
99100
@Optional()
100101
@Host()
@@ -129,56 +130,35 @@ export class BnaEditorComponent implements ControlValueAccessor {
129130
}
130131

131132
createEditor(initialContent: Block[]) {
133+
const blockSpecs = this.blockSpecs();
134+
const inlineContentSpecs = this.inlineContentSpecs();
135+
const styleSpecs = this.styleSpecs();
132136
this.editor = BlockNoteEditor.create({
133137
trailingBlock: false,
134138
schema: BlockNoteSchema.create({
135-
blockSpecs: {
136-
// enable the default blocks if desired
137-
...defaultBlockSpecs,
138-
139-
// Add your own custom blocks:
140-
alert: alertBlock,
141-
},
142-
inlineContentSpecs: {
143-
// enable the default inline content if desired
144-
...defaultInlineContentSpecs,
145-
146-
// Add your own custom inline content:
147-
// customInlineContent: CustomInlineContent,
148-
},
149-
styleSpecs: {
150-
// enable the default styles if desired
151-
...defaultStyleSpecs,
152-
153-
// Add your own custom styles:
154-
// customStyle: CustomStyle
155-
},
139+
blockSpecs: blockSpecs ? blockSpecs : { ...defaultBlockSpecs },
140+
inlineContentSpecs: inlineContentSpecs
141+
? inlineContentSpecs
142+
: { ...defaultInlineContentSpecs },
143+
styleSpecs: styleSpecs
144+
? styleSpecs
145+
: {
146+
...defaultStyleSpecs,
147+
},
156148
}),
157149
initialContent: initialContent,
158-
//TODO: remove cast
159150
}) as unknown as BlockNoteEditor;
160151
this.slashMenuItems = this.getSlashMenuItems(this.editor);
161152
this.editor.onChange((data) => {
162153
this.onChange(data.document);
163154
});
164155
}
165156

166-
getSlashMenuItems(editor: BlockNoteEditor) {
167-
return [...getDefaultSlashMenuItems(editor)];
168-
}
169-
170-
addBlock() {
171-
// Block that the text cursor is currently in.
172-
// New block we want to insert.
173-
const helloWorldBlock: PartialBlock = {
174-
type: 'paragraph',
175-
content: [{ type: 'text', text: 'Hello World', styles: { bold: true } }],
176-
};
177-
178-
this.addedElement += 1;
179-
// Inserting the new block after the current one.
180-
insertOrUpdateBlock(this.editor, helloWorldBlock);
181-
this.editor.suggestionMenus.closeMenu();
157+
getSlashMenuItems(editor: BlockNoteEditor): DefaultSuggestionItem[] {
158+
const slashMenuItems = this.inputSlashMenuItems();
159+
return slashMenuItems
160+
? slashMenuItems
161+
: [...getDefaultSlashMenuItems(editor)];
182162
}
183163

184164
registerOnChange(fn: unknown) {
@@ -193,10 +173,4 @@ export class BnaEditorComponent implements ControlValueAccessor {
193173
this.isDisabled = isDisabled;
194174
this.editor.isEditable = !isDisabled;
195175
}
196-
197-
toggleStyle($event: MouseEvent, style: string) {
198-
$event.preventDefault();
199-
$event.stopPropagation();
200-
this.editor.toggleStyles({ [style]: true });
201-
}
202176
}

0 commit comments

Comments
 (0)