@@ -18,7 +18,6 @@ import {
18
18
BlockNoteEditor ,
19
19
getBlockInfoFromPos ,
20
20
HyperlinkToolbarProps ,
21
- PartialBlock ,
22
21
} from './blocknote'
23
22
import { getNodeById } from './blocknote/core/api/util/nodeUtil'
24
23
import { HypermediaLinkForm } from './hm-link-form'
@@ -148,18 +147,23 @@ export function HypermediaLinkSwitchToolbar(
148
147
view . dispatch ( tr )
149
148
}
150
149
} 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
+ )
163
167
}
164
168
props . resetHyperlink ( )
165
169
} }
@@ -197,65 +201,44 @@ export function HypermediaLinkSwitchToolbar(
197
201
const buttonTitle = getTitle ( unpackedRef , entity . data ?. document )
198
202
if ( buttonTitle ) title = buttonTitle
199
203
}
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
+ } )
206
209
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
+ )
225
218
} }
226
219
active = { props . type === 'button' }
227
220
/>
228
221
< LinkSwitchButton
229
222
tooltipText = "Change to an embed"
230
223
icon = { PanelBottom }
231
224
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
+ )
241
233
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
+ )
259
242
} }
260
243
active = { props . type === 'embed' }
261
244
/>
@@ -321,6 +304,7 @@ function getTitle(
321
304
322
305
function insertNode (
323
306
editor : BlockNoteEditor < HMBlockSchema > ,
307
+ selectedId : string ,
324
308
link : string ,
325
309
text : string ,
326
310
prevType : string ,
@@ -387,8 +371,13 @@ function insertNode(
387
371
)
388
372
tr = tr . deleteRange ( startPos , $pos . end ( ) )
389
373
} 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
+ )
392
381
}
393
382
view . dispatch ( tr )
394
383
editor . _tiptapEditor . commands . focus ( )
@@ -398,12 +387,12 @@ function insertMentionNode(
398
387
editor : BlockNoteEditor < HMBlockSchema > ,
399
388
name : string ,
400
389
node : Node ,
401
- id : string ,
390
+ selectedId : string ,
402
391
inline : boolean ,
403
392
) {
404
393
const { state, view} = editor . _tiptapEditor
405
394
let tr = state . tr
406
- const { posBeforeNode} = getNodeById ( id , state . doc )
395
+ const { posBeforeNode} = getNodeById ( selectedId , state . doc )
407
396
408
397
const $pos = state . doc . resolve ( posBeforeNode + 1 )
409
398
let startPos = $pos . start ( )
0 commit comments