This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rich-text-editor): link extension (#991)
- Loading branch information
1 parent
2427f39
commit d88e704
Showing
9 changed files
with
516 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { | ||
combineTransactionSteps, | ||
findChildrenInRange, | ||
getChangedRanges, | ||
} from '@tiptap/core'; | ||
import { | ||
Plugin, | ||
PluginKey, | ||
} from '@tiptap/pm/state'; | ||
import { | ||
addMarks, | ||
removeMarks, | ||
} from './utils'; | ||
|
||
/** | ||
* Plugin to automatically add links into content. | ||
*/ | ||
export function autolink (options) { | ||
// Flag to see if we've loaded this plugin once already. This is used to run | ||
// the initial content through the plugin if the editor was mounted with some. | ||
let hasInitialized = false; | ||
|
||
return new Plugin({ | ||
key: new PluginKey('autolink'), | ||
|
||
appendTransaction: (transactions, oldState, newState) => { | ||
const contentChanged = transactions.some(tr => tr.docChanged) && | ||
!oldState.doc.eq(newState.doc); | ||
|
||
// Every interaction with the editor is a transaction, but we only care | ||
// about the ones with content changes. | ||
if (hasInitialized && !contentChanged) { | ||
return; | ||
} | ||
|
||
// The original transaction that we're manipulating. | ||
const { tr } = newState; | ||
|
||
// Text content after the original transaction. | ||
const { textContent } = newState.doc; | ||
|
||
// When the editor is initialized we want to add links to it. | ||
if (!hasInitialized) { | ||
addMarks(textContent, 0, 0, textContent.length, tr, options.type); | ||
} | ||
|
||
hasInitialized = true; | ||
|
||
// The transformed state of the document. | ||
const transform = combineTransactionSteps( | ||
oldState.doc, | ||
[...transactions], | ||
); | ||
|
||
// All the changes within the document. | ||
const changes = getChangedRanges(transform); | ||
|
||
changes.forEach(({ oldRange, newRange }) => { | ||
// Remove all link marks in the changed range since we'll add them | ||
// right back if they're still valid links. | ||
removeMarks(newRange, newState.doc, tr, options.type); | ||
|
||
// Find all paragraphs (Textblocks) that were affected since we want to | ||
// handle matches in each paragraph separately. | ||
const paragraphs = findChildrenInRange( | ||
newState.doc, | ||
newRange, | ||
node => node.isTextblock, | ||
); | ||
|
||
paragraphs.forEach(({ node, pos }) => { | ||
addMarks( | ||
node.textContent, | ||
pos, | ||
oldRange.from, | ||
newRange.to, | ||
tr, | ||
options.type, | ||
); | ||
}); | ||
}); | ||
|
||
// Return the modified transaction or the changes above wont have effect. | ||
return tr; | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Link } from './link'; | ||
|
||
export * from './link'; | ||
|
||
export default Link; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
mergeAttributes, | ||
Mark, | ||
} from '@tiptap/core'; | ||
import { autolink } from './autolink'; | ||
|
||
const defaultAttributes = { | ||
class: 'd-link d-c-text d-d-inline-block', | ||
rel: 'noopener noreferrer nofollow', | ||
}; | ||
|
||
// This is the actual extension code, which is mostly showing that all the | ||
// functionality comes from the ProseMirror plugin. | ||
export const Link = Mark.create({ | ||
name: 'Link', | ||
|
||
renderHTML ({ HTMLAttributes }) { | ||
return [ | ||
'a', | ||
mergeAttributes( | ||
this.options.HTMLAttributes, | ||
HTMLAttributes, | ||
defaultAttributes, | ||
), | ||
0, | ||
]; | ||
}, | ||
|
||
renderText ({ node }) { | ||
return node.attrs.text; | ||
}, | ||
|
||
addProseMirrorPlugins () { | ||
return [ | ||
autolink({ type: this.type }), | ||
]; | ||
}, | ||
}); |
Oops, something went wrong.