Skip to content

Commit

Permalink
Apply links correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Feb 20, 2024
1 parent ce1e5b0 commit 9c68f6d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/core/shape/RichTextShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,19 @@ export class RichTextShape<Blocks>
}
async applyLinks(doc: TextDoc<Blocks>, loader: LinkResolver): Promise<void> {
if (!Array.isArray(doc)) return
doc = doc.map(this.normalizeRow)
const links = new Map<Mark, string>()
iterMarks(doc, mark => {
if (mark[Mark.type] !== 'link') return
const id = mark[LinkMark.id]
if (id) links.set(mark, id)
const entryId = mark[LinkMark.entry]
if (entryId) links.set(mark, entryId)
})
async function loadLinks() {
const linkIds = Array.from(new Set(links.values()))
const entries = await loader.resolveLinks(linkInfoFields, linkIds)
const info = new Map(linkIds.map((id, i) => [id, entries[i]]))
for (const [mark, id] of links) {
for (const [mark, entryId] of links) {
const type = mark![LinkMark.link] as 'entry' | 'file' | undefined
const data = info.get(id)
const data = info.get(entryId)
if (data) mark!['href'] = type === 'file' ? data.location : data.url
}
}
Expand Down
1 change: 0 additions & 1 deletion src/field/richtext/RichTextField.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ export function RichTextInput<Blocks extends Schema>({
}),
[fragment]
)
console.log(content)
const onFocus = useCallback(
({event}: {event: Event}) => focusToggle(event.currentTarget),
[focusToggle]
Expand Down
7 changes: 6 additions & 1 deletion src/ui/RichText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ function nodeElement(
case 'small':
return <small />
case 'link':
return <a {...attributes} />
const props = {
href: attributes?.href,
target: attributes?.target,
title: attributes?.title
}
return <a {...props} />
}
}

Expand Down

0 comments on commit 9c68f6d

Please sign in to comment.