2025-09-21 11:02:31 -07:00
|
|
|
"use client";
|
2024-11-01 15:31:54 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
import { defaultMarkdownSerializer } from "prosemirror-markdown";
|
|
|
|
|
import { DOMParser, type Node } from "prosemirror-model";
|
|
|
|
|
import { Decoration, DecorationSet, type EditorView } from "prosemirror-view";
|
|
|
|
|
import { renderToString } from "react-dom/server";
|
2024-11-01 15:31:54 +05:30
|
|
|
|
2026-03-13 13:12:33 -07:00
|
|
|
import { MessageResponse } from "@/components/ai-elements/message";
|
2024-11-01 15:31:54 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
import { documentSchema } from "./config";
|
2026-03-20 09:37:02 +00:00
|
|
|
import type { UISuggestion } from "./suggestions";
|
2024-11-01 15:31:54 +05:30
|
|
|
|
|
|
|
|
export const buildDocumentFromContent = (content: string) => {
|
|
|
|
|
const parser = DOMParser.fromSchema(documentSchema);
|
2026-03-13 13:12:33 -07:00
|
|
|
const stringFromMarkdown = renderToString(
|
|
|
|
|
<MessageResponse>{content}</MessageResponse>
|
|
|
|
|
);
|
2025-09-21 11:02:31 -07:00
|
|
|
const tempContainer = document.createElement("div");
|
2024-11-01 15:31:54 +05:30
|
|
|
tempContainer.innerHTML = stringFromMarkdown;
|
|
|
|
|
return parser.parse(tempContainer);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const buildContentFromDocument = (document: Node) => {
|
|
|
|
|
return defaultMarkdownSerializer.serialize(document);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const createDecorations = (
|
2025-09-21 11:02:31 -07:00
|
|
|
suggestions: UISuggestion[],
|
2026-03-20 09:37:02 +00:00
|
|
|
_view: EditorView
|
2024-11-01 15:31:54 +05:30
|
|
|
) => {
|
2025-09-21 11:02:31 -07:00
|
|
|
const decorations: Decoration[] = [];
|
2024-11-01 15:31:54 +05:30
|
|
|
|
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,
|
|
|
|
|
{
|
2025-09-21 11:02:31 -07:00
|
|
|
class: "suggestion-highlight",
|
2026-03-20 09:37:02 +00:00
|
|
|
"data-suggestion-id": suggestion.id,
|
2024-11-01 15:31:54 +05:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
suggestionId: suggestion.id,
|
2025-09-21 11:02:31 -07:00
|
|
|
type: "highlight",
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-11-01 15:31:54 +05:30
|
|
|
);
|
2024-11-15 12:18:17 -05:00
|
|
|
}
|
2024-11-01 15:31:54 +05:30
|
|
|
|
2026-03-20 09:37:02 +00:00
|
|
|
return DecorationSet.create(_view.state.doc, decorations);
|
2024-11-01 15:31:54 +05:30
|
|
|
};
|