Skip to content

Commit

Permalink
fix: check for selected block type with index
Browse files Browse the repository at this point in the history
  • Loading branch information
m-risto committed Sep 17, 2024
1 parent c208364 commit 9aaf77e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
<hlm-menu>
<hlm-menu-group class="px-2">
<div class="flex flex-col w-full">
@for (blockType of filteredBlockTypes(); track blockType.name) {
@for (
blockType of filteredBlockTypes();
track blockType.name;
let index = $index
) {
<button
type="button"
hlmMenuItemCheckbox
class="gap-2 flex justify-start"
(click)="changeBlockType(blockType.type, blockType.props)"
[checked]="currentBlockType() === blockType"
[checked]="currentBlockTypeIndex() === index"
>
<hlm-menu-item-check />
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommonModule } from '@angular/common';
import { Component, computed, input, signal } from '@angular/core';
import { Dictionary } from '@blocknote/core';
import { BlockNoteEditor, Dictionary } from '@blocknote/core';
import { provideIcons } from '@ng-icons/core';
import {
lucideCheck,
Expand Down Expand Up @@ -88,12 +88,12 @@ export class BnaBlockTypeSelectComponent {
(item) => item.type in editor.schema.blockSchema,
);
});
currentBlockType = signal<BlockTypeSelectItem | undefined>(
this.filteredBlockTypes().find((a) =>
a.isSelected(
this.ngxBlockNoteService.editor().getTextCursorPosition().block,
),
),
currentBlockType = computed(() => {
const index = this.currentBlockTypeIndex();
return index !== undefined ? this.filteredBlockTypes()[index] : undefined;
});
currentBlockTypeIndex = signal<number | undefined>(
this.getCurrentBlockIndex(this.ngxBlockNoteService.editor()),
);

constructor(private ngxBlockNoteService: NgxBlocknoteService) {
Expand All @@ -103,14 +103,16 @@ export class BnaBlockTypeSelectComponent {
private updateCurrentBlockTypeOnChanges() {
const editor = this.ngxBlockNoteService.editor();
useEditorContentOrSelectionChange(() => {
this.currentBlockType.set(
this.filteredBlockTypes().find((a) =>
a.isSelected(editor.getTextCursorPosition().block),
),
);
this.currentBlockTypeIndex.set(this.getCurrentBlockIndex(editor));
}, editor);
}

private getCurrentBlockIndex(editor: BlockNoteEditor<any, any, any>) {
return this.filteredBlockTypes().findIndex((a) =>
a.isSelected(editor.getTextCursorPosition().block),
);
}

changeBlockType(
type: string,
props?: Record<string, boolean | number | string> | undefined,
Expand Down

0 comments on commit 9aaf77e

Please sign in to comment.