Skip to content

Commit 7ecf783

Browse files
committed
fixed switch toolbar deleting child blocks of the block
1 parent de8c2f6 commit 7ecf783

File tree

2 files changed

+57
-68
lines changed

2 files changed

+57
-68
lines changed

frontend/packages/editor/src/blocknote/core/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export const KeyboardShortcutsExtension = Extension.create<{
316316

317317
return false
318318
}),
319-
// Removes a level of nesting if the block is indented if the selection is at the start of the block.
319+
// Removes a level of nesting if the block is indented and the selection is at the start of the block.
320320
() =>
321321
commands.command(({state}) => {
322322
const {blockContent} = getBlockInfoFromSelection(state)

frontend/packages/editor/src/hm-link-switch-toolbar.tsx

+56-67
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
BlockNoteEditor,
1919
getBlockInfoFromPos,
2020
HyperlinkToolbarProps,
21-
PartialBlock,
2221
} from './blocknote'
2322
import {getNodeById} from './blocknote/core/api/util/nodeUtil'
2423
import {HypermediaLinkForm} from './hm-link-form'
@@ -148,18 +147,23 @@ export function HypermediaLinkSwitchToolbar(
148147
view.dispatch(tr)
149148
}
150149
} else {
151-
const linkBlock = {
152-
type: 'paragraph',
153-
props: {},
154-
content: [
155-
{
156-
type: 'link',
157-
href: props.url,
158-
content: title,
159-
},
160-
],
161-
} as PartialBlock<HMBlockSchema>
162-
props.editor.replaceBlocks([props.id], [linkBlock])
150+
const {state} = props.editor._tiptapEditor
151+
const node = state.schema.nodes.paragraph.create(
152+
null,
153+
state.schema.text(
154+
title,
155+
// @ts-ignore
156+
state.schema.marks['link'].create({href: props.url})!,
157+
),
158+
)
159+
insertNode(
160+
props.editor,
161+
props.id,
162+
props.url,
163+
props.text,
164+
props.type,
165+
node,
166+
)
163167
}
164168
props.resetHyperlink()
165169
}}
@@ -197,65 +201,44 @@ export function HypermediaLinkSwitchToolbar(
197201
const buttonTitle = getTitle(unpackedRef, entity.data?.document)
198202
if (buttonTitle) title = buttonTitle
199203
}
200-
if (['mention', 'link'].includes(props.type)) {
201-
const schema = props.editor._tiptapEditor.state.schema
202-
const node = schema.nodes.button.create({
203-
url: props.url,
204-
name: title,
205-
})
204+
const schema = props.editor._tiptapEditor.state.schema
205+
const node = schema.nodes.button.create({
206+
url: props.url,
207+
name: title,
208+
})
206209

207-
insertNode(
208-
props.editor,
209-
props.url,
210-
props.text,
211-
props.type,
212-
node,
213-
)
214-
} else {
215-
const buttonBlock = {
216-
type: 'button',
217-
content: [],
218-
props: {
219-
url: props.url,
220-
name: title,
221-
},
222-
} as PartialBlock<HMBlockSchema>
223-
props.editor.replaceBlocks([props.id], [buttonBlock])
224-
}
210+
insertNode(
211+
props.editor,
212+
props.id,
213+
props.url,
214+
props.text,
215+
props.type,
216+
node,
217+
)
225218
}}
226219
active={props.type === 'button'}
227220
/>
228221
<LinkSwitchButton
229222
tooltipText="Change to an embed"
230223
icon={PanelBottom}
231224
onPress={() => {
232-
if (['mention', 'link'].includes(props.type)) {
233-
const schema = props.editor._tiptapEditor.state.schema
234-
const node = schema.nodes.embed.create(
235-
{
236-
url: props.url,
237-
view: 'Content',
238-
},
239-
schema.text(' '),
240-
)
225+
const schema = props.editor._tiptapEditor.state.schema
226+
const node = schema.nodes.embed.create(
227+
{
228+
url: props.url,
229+
view: 'Content',
230+
},
231+
schema.text(' '),
232+
)
241233

242-
insertNode(
243-
props.editor,
244-
props.url,
245-
props.text,
246-
props.type,
247-
node,
248-
)
249-
} else {
250-
const embedBlock = {
251-
type: 'embed',
252-
content: [],
253-
props: {
254-
url: props.url,
255-
},
256-
} as PartialBlock<HMBlockSchema>
257-
props.editor.replaceBlocks([props.id], [embedBlock])
258-
}
234+
insertNode(
235+
props.editor,
236+
props.id,
237+
props.url,
238+
props.text,
239+
props.type,
240+
node,
241+
)
259242
}}
260243
active={props.type === 'embed'}
261244
/>
@@ -321,6 +304,7 @@ function getTitle(
321304

322305
function insertNode(
323306
editor: BlockNoteEditor<HMBlockSchema>,
307+
selectedId: string,
324308
link: string,
325309
text: string,
326310
prevType: string,
@@ -387,8 +371,13 @@ function insertNode(
387371
)
388372
tr = tr.deleteRange(startPos, $pos.end())
389373
} else {
390-
const $pos = state.doc.resolve($from.pos)
391-
tr = tr.replaceWith($pos.start() - 2, $pos.end(), node)
374+
const {posBeforeNode} = getNodeById(selectedId, state.doc)
375+
const blockInfo = getBlockInfoFromPos(state, posBeforeNode + 1)
376+
tr = tr.replaceRangeWith(
377+
blockInfo.blockContent.beforePos,
378+
blockInfo.blockContent.afterPos,
379+
node,
380+
)
392381
}
393382
view.dispatch(tr)
394383
editor._tiptapEditor.commands.focus()
@@ -398,12 +387,12 @@ function insertMentionNode(
398387
editor: BlockNoteEditor<HMBlockSchema>,
399388
name: string,
400389
node: Node,
401-
id: string,
390+
selectedId: string,
402391
inline: boolean,
403392
) {
404393
const {state, view} = editor._tiptapEditor
405394
let tr = state.tr
406-
const {posBeforeNode} = getNodeById(id, state.doc)
395+
const {posBeforeNode} = getNodeById(selectedId, state.doc)
407396

408397
const $pos = state.doc.resolve(posBeforeNode + 1)
409398
let startPos = $pos.start()

0 commit comments

Comments
 (0)