Skip to content

Commit 86fc9fa

Browse files
committed
feat: add styling for upload and embed file
1 parent 896157d commit 86fc9fa

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1-
<div class="bg-white">
2-
<input class="w-80" hlmInput type="file" (input)="onFileInput($event)" />
3-
<!-- <input class="w-80" hlmInput [(ngModel)]="embedInputText" type="text" />-->
4-
<!-- <button hlmBtn (click)="insertEmbed()" >Embed Image</button>-->
1+
<div class="bg-background shadow-2xl rounded shadow-neutral-700 w-96">
2+
<hlm-tabs tab="upload">
3+
<hlm-tabs-list class="w-full grid grid-cols-2" aria-label="file panel">
4+
<button hlmTabsTrigger="upload">Upload</button>
5+
<button hlmTabsTrigger="embed">Embed</button>
6+
</hlm-tabs-list>
7+
<div hlmTabsContent="upload" class="p-3">
8+
<input
9+
class="w-full"
10+
hlmInput
11+
type="file"
12+
(input)="onFileInputChanged($event)"
13+
/>
14+
</div>
15+
<div hlmTabsContent="embed" class="p-3">
16+
<form (submit)="insertEmbedFile(embedInputText)">
17+
<input
18+
class="w-full mb-3"
19+
placeholder="Enter URL"
20+
[(ngModel)]="embedInputText"
21+
hlmInput
22+
type="text"
23+
[ngModelOptions]="{ standalone: true }"
24+
/>
25+
<button hlmBtn variant="default" class="w-full">Embed</button>
26+
</form>
27+
</div>
28+
</hlm-tabs>
529
</div>

libs/block-note-angular/src/lib/components/bna-file-panel/bna-file-panel.component.ts

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import {
22
Component,
3-
Directive,
43
ElementRef,
54
input,
65
OnChanges,
7-
Renderer2
6+
Renderer2,
87
} from '@angular/core';
8+
import { FormsModule } from '@angular/forms';
99
import {
1010
BlockFromConfig,
1111
BlockNoteEditor,
12-
FileBlockConfig
12+
FileBlockConfig,
1313
} from '@blocknote/core';
14-
import { autoUpdate, computePosition, flip } from '@floating-ui/dom';
15-
import { getVirtualElement } from '../../util/get-virtual-element.util';
14+
import { autoUpdate, computePosition, flip, offset } from '@floating-ui/dom';
15+
import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
1616
import { HlmInputDirective } from '@spartan-ng/ui-input-helm';
1717
import {
1818
HlmTabsComponent,
19-
HlmTabsListComponent
19+
HlmTabsContentDirective,
20+
HlmTabsListComponent,
21+
HlmTabsTriggerDirective,
2022
} from '@spartan-ng/ui-tabs-helm';
21-
import { HlmButtonDirective } from '@spartan-ng/ui-button-helm';
22-
import { FormsModule } from '@angular/forms';
23+
import { getVirtualElement } from '../../util/get-virtual-element.util';
2324

2425
@Component({
2526
selector: 'bna-file-panel',
@@ -29,12 +30,14 @@ import { FormsModule } from '@angular/forms';
2930
HlmTabsComponent,
3031
HlmTabsListComponent,
3132
HlmButtonDirective,
32-
FormsModule
33+
FormsModule,
34+
HlmTabsTriggerDirective,
35+
HlmTabsContentDirective,
3336
],
3437
templateUrl: './bna-file-panel.component.html',
3538
})
3639
export class BnaFilePanelComponent implements OnChanges {
37-
editor = input.required<BlockNoteEditor<any,any,any>>();
40+
editor = input.required<BlockNoteEditor<any, any, any>>();
3841
private block?: BlockFromConfig<FileBlockConfig, any, any>;
3942

4043
embedInputText = '';
@@ -59,14 +62,14 @@ export class BnaFilePanelComponent implements OnChanges {
5962
if (!filePanelState.show) {
6063
cleanup();
6164
} else {
62-
this.block = filePanelState.block
65+
this.block = filePanelState.block;
6366
const updatePosition = async () => {
6467
const result = await computePosition(
6568
getVirtualElement(filePanelState.referencePos),
6669
this.elRef.nativeElement,
6770
{
68-
placement: 'bottom-start',
69-
middleware: [flip()],
71+
placement: 'bottom',
72+
middleware: [offset(10), flip()],
7073
}
7174
);
7275
this.renderer2.setStyle(
@@ -100,44 +103,39 @@ export class BnaFilePanelComponent implements OnChanges {
100103
}
101104
}
102105

103-
async onFileInput(event: Event){
104-
const editor = this.editor()
105-
if (!editor.uploadFile || !this.block){
106+
async onFileInputChanged(event: Event) {
107+
const editor = this.editor();
108+
if (!editor.uploadFile || !this.block) {
106109
console.error('uploadFile was not provided in editor options');
107-
return
110+
return;
108111
}
109112

110-
const files = (event.target as HTMLInputElement).files
111-
if(!files) return
112-
113-
const file = files[0]
114-
const fileUrl = await editor.uploadFile(file)
113+
const files = (event.target as HTMLInputElement).files;
114+
if (!files) return;
115115

116-
editor.updateBlock(
117-
this.block,
118-
{
119-
props: {
120-
url: fileUrl,
121-
},
122-
}
123-
)
116+
const file = files[0];
117+
const fileUrl = await editor.uploadFile(file);
124118

125-
editor.filePanel?.closeMenu()
119+
this.updateBlockWithEmbedFileUrl(this.block, editor, fileUrl);
126120
}
127121

128-
insertEmbed(){
129-
const editor = this.editor()
130-
if(!this.block) return;
122+
insertEmbedFile(embedFileUrl: string) {
123+
const editor = this.editor();
124+
if (!this.block) return;
125+
this.updateBlockWithEmbedFileUrl(this.block, editor, embedFileUrl);
126+
}
131127

132-
editor.updateBlock(
133-
this.block,
134-
{
135-
props: {
136-
url: this.embedInputText,
137-
},
138-
}
139-
)
128+
private updateBlockWithEmbedFileUrl(
129+
block: BlockFromConfig<FileBlockConfig, any, any>,
130+
editor: BlockNoteEditor<any, any, any>,
131+
embedFileUrl: string | Record<string, any>
132+
) {
133+
editor.updateBlock(block, {
134+
props: {
135+
url: embedFileUrl,
136+
},
137+
});
140138

141-
editor.filePanel?.closeMenu()
139+
editor.filePanel?.closeMenu();
142140
}
143141
}

0 commit comments

Comments
 (0)