Skip to content

Commit 088d9d4

Browse files
committed
feat: add saving and loading
1 parent 35fb92e commit 088d9d4

File tree

5 files changed

+147
-3
lines changed

5 files changed

+147
-3
lines changed

docs/src/app/app.routes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212
import {
1313
RemovingDefaultBlocksPage
1414
} from '../pages/examples/basic/removing-default-blocks/removing-default-blocks.page';
15+
import {
16+
SavingAndLoadingPage
17+
} from '../pages/examples/backend/saving-and-loading/saving-and-loading.page';
1518

1619
export const appRoutes: Route[] = [
1720
{ path: '', redirectTo: 'overview', pathMatch: 'full' },
@@ -21,7 +24,6 @@ export const appRoutes: Route[] = [
2124
component: ExamplesPage,
2225
children: [
2326
{ path: '', redirectTo: 'basic/minimal', pathMatch: 'full' },
24-
2527
{ path: 'basic/minimal', component: BasicSetupPage },
2628
{ path: 'basic/blocks-json', component: BlocksJsonPage },
2729
{
@@ -36,6 +38,7 @@ export const appRoutes: Route[] = [
3638
path: 'basic/removing-default-blocks',
3739
component: RemovingDefaultBlocksPage,
3840
},
41+
{ path: 'backend/saving-and-loading', component: SavingAndLoadingPage },
3942
{
4043
path: 'custom/alert-block',
4144
component: AlertBlockPage,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { CommonModule } from '@angular/common';
2+
import { Component, OnInit } from '@angular/core';
3+
import { BnaEditorComponent } from '@dytab/block-note-angular';
4+
import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
5+
import {
6+
Block, BlockNoteEditor,
7+
PartialBlock
8+
} from '@blocknote/core';
9+
10+
@Component({
11+
selector: 'bna-saving-and-loading-example',
12+
standalone: true,
13+
imports: [CommonModule, BnaEditorComponent, HlmButtonDirective],
14+
template: `
15+
<bna-editor [initialContent]="initialContent" (contentChanged)="saveToStorage($event)" />
16+
`,
17+
})
18+
export class SavingAndLoadingExample implements OnInit {
19+
initialContent = this.loadFromStorage();
20+
21+
ngOnInit(): void {
22+
BlockNoteEditor.create({ initialContent: this.initialContent });
23+
}
24+
25+
saveToStorage(jsonBlocks: Block[]) {
26+
// Save contents to local storage. You might want to debounce this or replace
27+
// with a call to your API / database.
28+
localStorage.setItem("editorContent", JSON.stringify(jsonBlocks));
29+
}
30+
31+
private loadFromStorage() {
32+
// Gets the previously stored editor contents.
33+
const storageString = localStorage.getItem("editorContent");
34+
return storageString
35+
? (JSON.parse(storageString) as PartialBlock[])
36+
: undefined;
37+
}
38+
}
39+
40+
export const savingAndLoadingExampleCode = `import { CommonModule } from '@angular/common';
41+
import { Component, OnInit } from '@angular/core';
42+
import { BnaEditorComponent } from '@dytab/block-note-angular';
43+
import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
44+
import {
45+
Block, BlockNoteEditor,
46+
PartialBlock
47+
} from '@blocknote/core';
48+
49+
@Component({
50+
selector: 'bna-saving-and-loading-example',
51+
standalone: true,
52+
imports: [CommonModule, BnaEditorComponent, HlmButtonDirective],
53+
template: \`
54+
<bna-editor [initialContent]="initialContent" (contentChanged)="saveToStorage($event)" />
55+
\`,
56+
})
57+
export class SavingAndLoadingExample implements OnInit {
58+
initialContent = this.loadFromStorage();
59+
60+
ngOnInit(): void {
61+
BlockNoteEditor.create({ initialContent: this.initialContent });
62+
}
63+
64+
saveToStorage(jsonBlocks: Block[]) {
65+
// Save contents to local storage. You might want to debounce this or replace
66+
// with a call to your API / database.
67+
localStorage.setItem("editorContent", JSON.stringify(jsonBlocks));
68+
}
69+
70+
private loadFromStorage() {
71+
// Gets the previously stored editor contents.
72+
const storageString = localStorage.getItem("editorContent");
73+
return storageString
74+
? (JSON.parse(storageString) as PartialBlock[])
75+
: undefined;
76+
}
77+
}`;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { CommonModule } from '@angular/common';
2+
import { Component } from '@angular/core';
3+
import {
4+
HlmTabsComponent,
5+
HlmTabsContentDirective,
6+
HlmTabsListComponent,
7+
HlmTabsTriggerDirective,
8+
} from '@spartan-ng/ui-tabs-helm';
9+
import { hlmP } from '@spartan-ng/ui-typography-helm';
10+
import { Highlight } from 'ngx-highlightjs';
11+
import { CodeComponent } from '../../../../shared/code/code.component';
12+
import { DemoBoxComponent } from '../../../../shared/layout/demo-box.component';
13+
import { TabsComponent } from '../../../../shared/layout/example-tabs.component';
14+
import { SectionIntroComponent } from '../../../../shared/layout/section-intro.component';
15+
import {
16+
SavingAndLoadingExample,
17+
savingAndLoadingExampleCode,
18+
} from './saving-and-loading.example';
19+
20+
@Component({
21+
standalone: true,
22+
imports: [
23+
CommonModule,
24+
SectionIntroComponent,
25+
DemoBoxComponent,
26+
HlmTabsComponent,
27+
HlmTabsListComponent,
28+
HlmTabsContentDirective,
29+
HlmTabsTriggerDirective,
30+
TabsComponent,
31+
CodeComponent,
32+
SavingAndLoadingExample,
33+
Highlight,
34+
],
35+
template: `
36+
<bna-section-intro name="Saving & Loading">
37+
<p class="${hlmP} mb-8">
38+
This example shows how to save the editor contents to local storage whenever a change is made, and load the saved contents when the editor is created.
39+
<br><br>
40+
You can replace the <code>saveToStorage</code> and <code>loadFromStorage</code> functions with calls to your backend or database.
41+
<br><br>
42+
<b>Try it out:</b> Try typing in the editor and reloading the page!
43+
</p>
44+
</bna-section-intro>
45+
<hlm-tabs tab="preview">
46+
<bna-example-tabs firstTab="Preview" secondTab="Code">
47+
<bna-demo-box firstTab>
48+
<bna-saving-and-loading-example />
49+
</bna-demo-box>
50+
<bna-code [code]="exampleCode" secondTab />
51+
</bna-example-tabs>
52+
</hlm-tabs>
53+
`,
54+
})
55+
export class SavingAndLoadingPage{
56+
exampleCode = savingAndLoadingExampleCode;
57+
}

docs/src/pages/examples/examples.page.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
4343
class="justify-start"
4444
>Default Schema Showcase</a
4545
>
46+
47+
Backend
48+
<a hlmBtn variant="ghost" routerLink="backend/saving-and-loading" class="justify-start"
49+
>Saving & Loading</a
50+
>
51+
4652
Custom
4753
<a
4854
hlmBtn
@@ -51,6 +57,7 @@ import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
5157
routerLink="custom/alert-block"
5258
>Alert Block</a
5359
>
60+
5461
Interoperability
5562
<a
5663
hlmBtn

libs/block-note-angular/src/lib/block-note-editor/bna-editor.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
defaultStyleSpecs,
1717
DefaultSuggestionItem,
1818
getDefaultSlashMenuItems,
19-
InlineContentSpecs,
19+
InlineContentSpecs, PartialBlock,
2020
StyleSpecs
2121
} from '@blocknote/core';
2222
import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
@@ -80,7 +80,7 @@ import {
8080
templateUrl: './bna-editor.component.html'
8181
})
8282
export class BnaEditorComponent implements OnChanges{
83-
initialContent = input<Block[]>();
83+
initialContent = input<Block[] | PartialBlock[]>();
8484
blockSpecs = input<BlockSpecs>();
8585
inlineContentSpecs = input<InlineContentSpecs>();
8686
styleSpecs = input<StyleSpecs>();

0 commit comments

Comments
 (0)