-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(quoteLink): add quote link additional plugin
- Loading branch information
kseniyakuzina
committed
Mar 5, 2025
1 parent
ed88851
commit 81b7420
Showing
22 changed files
with
599 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import {memo, useCallback} from 'react'; | ||
|
||
import {transform as quoteLink} from '@diplodoc/quote-link-extension'; | ||
import type {PluginWithParams} from 'markdown-it/lib'; | ||
|
||
import {QuoteLink as QuoteLinkExtension} from 'src/extensions/additional/QuoteLink'; | ||
import { | ||
MarkdownEditorView, | ||
type RenderPreview, | ||
mQuoteLinkItemData, | ||
markupToolbarConfigs, | ||
useMarkdownEditor, | ||
wQuoteLinkItemData, | ||
wysiwygToolbarConfigs, | ||
} from 'src/index'; | ||
import {cloneDeep} from 'src/lodash'; | ||
|
||
import {PlaygroundLayout} from '../../components/PlaygroundLayout'; | ||
import {SplitModePreview} from '../../components/SplitModePreview'; | ||
import {plugins as defaultPlugins} from '../../defaults/md-plugins'; | ||
import {useLogs} from '../../hooks/useLogs'; | ||
|
||
const wToolbarConfig = cloneDeep(wysiwygToolbarConfigs.wToolbarConfig); | ||
wToolbarConfig.push([wQuoteLinkItemData]); | ||
|
||
const wCommandMenuConfig = cloneDeep(wysiwygToolbarConfigs.wCommandMenuConfig); | ||
wCommandMenuConfig.push(wQuoteLinkItemData); | ||
|
||
const mToolbarConfig = cloneDeep(markupToolbarConfigs.mToolbarConfig); | ||
mToolbarConfig.push([mQuoteLinkItemData]); | ||
|
||
const plugins: PluginWithParams[] = [...defaultPlugins, quoteLink({bundle: false})]; | ||
|
||
export const QuoteLink = memo(() => { | ||
const wSelectionMenuConfig = [ | ||
[wQuoteLinkItemData], | ||
...wysiwygToolbarConfigs.wSelectionMenuConfig, | ||
]; | ||
|
||
const renderPreview = useCallback<RenderPreview>( | ||
({getValue, md}) => ( | ||
<SplitModePreview | ||
getValue={getValue} | ||
allowHTML={md.html} | ||
linkify={md.linkify} | ||
linkifyTlds={md.linkifyTlds} | ||
breaks={md.breaks} | ||
needToSanitizeHtml | ||
plugins={plugins} | ||
/> | ||
), | ||
[], | ||
); | ||
|
||
const editor = useMarkdownEditor({ | ||
initial: {markup: ''}, | ||
markupConfig: {renderPreview}, | ||
wysiwygConfig: { | ||
extensions: QuoteLinkExtension, | ||
extensionOptions: { | ||
commandMenu: { | ||
actions: wCommandMenuConfig, | ||
}, | ||
selectionContext: { | ||
config: wSelectionMenuConfig, | ||
}, | ||
yfmConfigs: { | ||
attrs: { | ||
allowedAttributes: ['data-quotelink'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
useLogs(editor.logger); | ||
|
||
return ( | ||
<PlaygroundLayout | ||
editor={editor} | ||
view={({className}) => ( | ||
<MarkdownEditorView | ||
autofocus | ||
stickyToolbar | ||
settingsVisible | ||
editor={editor} | ||
className={className} | ||
markupToolbarConfig={mToolbarConfig} | ||
wysiwygToolbarConfig={wToolbarConfig} | ||
/> | ||
)} | ||
/> | ||
); | ||
}); | ||
|
||
QuoteLink.displayName = 'GPT'; |
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,11 @@ | ||
import type {StoryObj} from '@storybook/react'; | ||
|
||
import {QuoteLink as component} from './QuoteLink'; | ||
|
||
export const Story: StoryObj<typeof component> = {}; | ||
Story.storyName = 'QuoteLink'; | ||
|
||
export default { | ||
title: 'Experiments / QuoteLink', | ||
component, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -41,6 +41,7 @@ const names = [ | |
'orderedList', | ||
'paragraph', | ||
'quote', | ||
'quoteLink', | ||
'redo', | ||
'sinkListItem', | ||
'strike', | ||
|
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
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
12 changes: 12 additions & 0 deletions
12
src/extensions/additional/QuoteLink/PlaceholderWidget/commands.ts
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,12 @@ | ||
import type {Command} from 'prosemirror-state'; | ||
|
||
import type {ExtensionDeps} from '#core'; | ||
|
||
import {addPlaceholder} from './descriptor'; | ||
|
||
export const addQuoteLinkPlaceholder = | ||
(deps: ExtensionDeps): Command => | ||
(state, dispatch) => { | ||
dispatch?.(addPlaceholder(state.tr, deps).scrollIntoView()); | ||
return true; | ||
}; |
97 changes: 97 additions & 0 deletions
97
src/extensions/additional/QuoteLink/PlaceholderWidget/descriptor.tsx
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,97 @@ | ||
import type React from 'react'; | ||
|
||
import type {Transaction} from 'prosemirror-state'; | ||
import {TextSelection} from 'prosemirror-state'; | ||
import type {EditorView} from 'prosemirror-view'; | ||
|
||
import type {ExtensionDeps} from '#core'; | ||
import { | ||
LinkAttr, | ||
ReactWidgetDescriptor, | ||
linkType, | ||
normalizeUrlFactory, | ||
pType, | ||
removeDecoration, | ||
} from 'src/extensions'; | ||
import { | ||
LinkPlaceholderWidget, | ||
type LinkPlaceholderWidgetProps, | ||
} from 'src/extensions/markdown/Link/PlaceholderWidget/widget'; | ||
import {isTextSelection} from 'src/utils'; | ||
|
||
export class QuoteLinkWidgetDescriptor extends ReactWidgetDescriptor { | ||
#domElem; | ||
#view?: EditorView; | ||
#getPos?: () => number; | ||
#schema?: ExtensionDeps['schema']; | ||
|
||
private normalizeUrl; | ||
|
||
constructor(initPos: number, deps: ExtensionDeps) { | ||
super(initPos, 'link-empty'); | ||
this.#domElem = document.createElement('span'); | ||
this.#schema = deps.schema; | ||
this.normalizeUrl = normalizeUrlFactory(deps); | ||
} | ||
|
||
getDomElem(): HTMLElement { | ||
return this.#domElem; | ||
} | ||
|
||
renderReactElement(view: EditorView, getPos: () => number): React.ReactElement { | ||
this.#view = view; | ||
this.#getPos = getPos; | ||
return <LinkPlaceholderWidget onCancel={this.onCancel} onSubmit={this.onSubmit} />; | ||
} | ||
|
||
onCancel: LinkPlaceholderWidgetProps['onCancel'] = () => { | ||
if (!this.#view) return; | ||
|
||
this.#view.dispatch(removeDecoration(this.#view.state.tr, this.id)); | ||
this.#view.focus(); | ||
}; | ||
|
||
onSubmit: LinkPlaceholderWidgetProps['onSubmit'] = (params) => { | ||
const normalizeResult = this.normalizeUrl(params.url); | ||
if (!normalizeResult || !this.#view || !this.#getPos) return; | ||
|
||
let tr = this.#view.state.tr; | ||
|
||
const {url} = normalizeResult; | ||
const text = params.text.trim() || normalizeResult.text; | ||
|
||
const from = this.#getPos(); | ||
const isAllSelected = | ||
from === 1 && (!isTextSelection(tr.selection) || !tr.selection.$cursor); | ||
const to = from + text.length + (isAllSelected ? 1 : 0); | ||
|
||
tr = tr.insertText(text, from); | ||
tr = tr.addMark( | ||
from, | ||
to, | ||
linkType(this.#view.state.schema).create({ | ||
[LinkAttr.Href]: url, | ||
[LinkAttr.DataQuoteLink]: true, | ||
}), | ||
); | ||
|
||
tr = removeDecoration(tr, this.id); | ||
|
||
tr = tr.insert( | ||
to, | ||
pType(this.#view.state.schema).create(null, text ? this.#schema?.text(text) : null), | ||
); | ||
tr.setSelection(TextSelection.create(tr.doc, to + 1 + text.length + 1)); | ||
|
||
this.#view.dispatch(tr); | ||
this.#view.focus(); | ||
}; | ||
} | ||
|
||
export const addPlaceholder = (tr: Transaction, deps: ExtensionDeps) => { | ||
const isAllSelected = | ||
tr.selection.from === 0 && (!isTextSelection(tr.selection) || !tr.selection.$cursor); | ||
return new QuoteLinkWidgetDescriptor(tr.selection.from + (isAllSelected ? 1 : 0), deps).applyTo( | ||
tr, | ||
); | ||
}; |
Oops, something went wrong.