2024-11-01 15:31:54 +05:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { defaultMarkdownSerializer } from 'prosemirror-markdown';
|
2024-11-15 12:18:17 -05:00
|
|
|
import { DOMParser, type Node } from 'prosemirror-model';
|
|
|
|
|
import { Decoration, DecorationSet, type EditorView } from 'prosemirror-view';
|
2024-11-01 15:31:54 +05:30
|
|
|
import { renderToString } from 'react-dom/server';
|
|
|
|
|
|
2025-08-28 14:15:36 +01:00
|
|
|
import { Response } from '@/components/elements/response';
|
2024-11-01 15:31:54 +05:30
|
|
|
|
|
|
|
|
import { documentSchema } from './config';
|
2024-11-15 12:18:17 -05:00
|
|
|
import { createSuggestionWidget, type UISuggestion } from './suggestions';
|
2024-11-01 15:31:54 +05:30
|
|
|
|
|
|
|
|
export const buildDocumentFromContent = (content: string) => {
|
|
|
|
|
const parser = DOMParser.fromSchema(documentSchema);
|
2025-08-28 14:15:36 +01:00
|
|
|
const stringFromMarkdown = renderToString(<Response>{content}</Response>);
|
2024-11-01 15:31:54 +05:30
|
|
|
const tempContainer = document.createElement('div');
|
|
|
|
|
tempContainer.innerHTML = stringFromMarkdown;
|
|
|
|
|
return parser.parse(tempContainer);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const buildContentFromDocument = (document: Node) => {
|
|
|
|
|
return defaultMarkdownSerializer.serialize(document);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const createDecorations = (
|
|
|
|
|
suggestions: Array<UISuggestion>,
|
2024-11-15 13:00:15 -05:00
|
|
|
view: EditorView,
|
2024-11-01 15:31:54 +05:30
|
|
|
) => {
|
|
|
|
|
const decorations: Array<Decoration> = [];
|
|
|
|
|
|
2024-11-15 12:18:17 -05:00
|
|
|
for (const suggestion of suggestions) {
|
2024-11-01 15:31:54 +05:30
|
|
|
decorations.push(
|
|
|
|
|
Decoration.inline(
|
|
|
|
|
suggestion.selectionStart,
|
|
|
|
|
suggestion.selectionEnd,
|
|
|
|
|
{
|
|
|
|
|
class: 'suggestion-highlight',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
suggestionId: suggestion.id,
|
|
|
|
|
type: 'highlight',
|
2024-11-15 13:00:15 -05:00
|
|
|
},
|
|
|
|
|
),
|
2024-11-01 15:31:54 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
decorations.push(
|
|
|
|
|
Decoration.widget(
|
|
|
|
|
suggestion.selectionStart,
|
|
|
|
|
(view) => {
|
|
|
|
|
const { dom } = createSuggestionWidget(suggestion, view);
|
|
|
|
|
return dom;
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
suggestionId: suggestion.id,
|
|
|
|
|
type: 'widget',
|
2024-11-15 13:00:15 -05:00
|
|
|
},
|
|
|
|
|
),
|
2024-11-01 15:31:54 +05:30
|
|
|
);
|
2024-11-15 12:18:17 -05:00
|
|
|
}
|
2024-11-01 15:31:54 +05:30
|
|
|
|
|
|
|
|
return DecorationSet.create(view.state.doc, decorations);
|
|
|
|
|
};
|