+
Skip to content

Fix link editor (for good) #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/app/editor/LinkExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const LinkExtension = Node.create({
name: "inlineLink",
group: "inline",
inline: true,
inclusive: false,
selectable: true,
draggable: true,
priority: 1000,
Expand Down Expand Up @@ -65,32 +66,36 @@ export const LinkExtension = Node.create({
return [
new InputRule({
find: text => {
const realText = this.editor.state.doc.textContent
const match = last(Array.from(text.matchAll(LINK_REGEX)))
if (match && text.length === match.index + match[0].length + 1) {
const realMatch = last(Array.from(realText.matchAll(LINK_REGEX)))
if (match && realMatch && text.length === match.index + match[0].length + 1) {
return {
index: match.index!,
text: match[0],
data: {
url: match[0],
url: match[0].trim(),
},
}
}

return null
},
handler: ({state, range, match}) => {
const {tr} = state
handler: ({range, match, chain}) => {
if (match[0]) {
try {
tr.insert(range.from - 1, this.type.create(match.data))
.delete(tr.mapping.map(range.from - 1), tr.mapping.map(range.to))
.insertText(last(Array.from(match.input!)))
} catch (e) {
// If the node was already linkified, the above code breaks for whatever reason
const lastChar = last(Array.from(match.input!))
const chainCommand = chain()
.deleteRange({from: range.from - 1, to: range.from - 1 + match[0].length})
.insertLink({url: match[0]})

if (lastChar === "\n") {
chainCommand.splitBlock()
} else {
chainCommand.insertContent(" ")
}
}

tr.scrollIntoView()
chainCommand.run()
}
},
}),
]
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载