Skip to content

Commit 223636c

Browse files
author
robin
committed
fix(editor-stacks/onChange-plugin): update import types and enhance content retrieval logic
1 parent 19b8134 commit 223636c

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

editor-stacks/onChange-plugin.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import { Plugin } from "prosemirror-state";
21-
import type { EditorPlugin } from "@stackoverflow/stacks-editor";
21+
import type { EditorPlugin, StacksEditor } from "@stackoverflow/stacks-editor";
2222

2323
/**
2424
* Creates a StacksEditor plugin that listens to content changes.
@@ -27,10 +27,12 @@ import type { EditorPlugin } from "@stackoverflow/stacks-editor";
2727
*
2828
* Works in both RichText and Markdown modes automatically.
2929
*
30+
* @param getEditor Function that returns the StacksEditor instance
3031
* @param onUpdate Callback function that receives the updated editor content
3132
* @returns A StacksEditor EditorPlugin
3233
*/
3334
export const createOnChangePlugin = (
35+
getEditor: () => StacksEditor | null,
3436
onUpdate: (content: string) => void
3537
): EditorPlugin => {
3638
return () => {
@@ -39,9 +41,14 @@ export const createOnChangePlugin = (
3941
const proseMirrorPlugin: any = new Plugin({
4042
view() {
4143
return {
42-
update(view) {
44+
update() {
4345
try {
44-
const content = view.state.doc.textContent;
46+
// Get the editor instance to access serialized markdown content
47+
const editor = getEditor();
48+
if (!editor) return;
49+
50+
// Use editor.content to get properly serialized markdown
51+
const content = editor.content;
4552

4653
// Only trigger callback if content actually changed
4754
if (content !== lastContent) {

0 commit comments

Comments
 (0)