Code Monkey home page Code Monkey logo

Comments (5)

janthurau avatar janthurau commented on September 26, 2024

@AnnieTJohn please use Discord for questions on how to do something. You can have a look at extension-database, or feel free to ask for help in Discord.

btw., when you get errors, don't hide them ("It shows query errors when sending the plain ydoc."). ;-)

from hocuspocus.

AnnieTJohn avatar AnnieTJohn commented on September 26, 2024

This does not show query errors.This is the node js server code dedicated to convert html to ydoc and save to rethinkDB. But it does not set content in editor. This is what we are facing. No errors shown:

import * as dotenv from "dotenv";
dotenv.config();
import { Server } from "@hocuspocus/server";
import { Database } from "@hocuspocus/extension-database";
import StarterKit from "@tiptap/starter-kit";
import { TiptapTransformer } from "@hocuspocus/transformer";
import { generateJSON } from "@tiptap/html";
//import rethinkdb from "rethinkdb";
import Image from "@tiptap/extension-image";

import Table from "@tiptap/extension-table";
import TableCell from "@tiptap/extension-table-cell";
import TableHeader from "@tiptap/extension-table-header";
import TableRow from "@tiptap/extension-table-row";
import express from "express";
import * as Y from "yjs";
import { fromUint8Array, toUint8Array } from "js-base64";

const app = express();
const port = process.env.HOCUSPOCUS_API_PORT;
//const port = 3030;

const CustomTableCell = TableCell.extend({
	addAttributes() {
		return {
			// extend the existing attributes …
			...this.parent?.(),

			// and add a new one …
			backgroundColor: {
				default: null,
				parseHTML: (element) =>
					element.getAttribute("data-background-color"),
				renderHTML: (attributes) => {
					return {
						"data-background-color": attributes.backgroundColor,
						style: `background-color: ${attributes.backgroundColor}`,
					};
				},
			},
		};
	},
});

app.post("/transform", express.json(), (req, res) => {
	const room = req.body.room;
	const html = req.body.template_content;
	console.log("Received: " + room);
	try {
		const ydoc = TiptapTransformer.toYdoc(
			generateJSON(html, [
				StarterKit,
				Table.configure({
					resizable: true,
				}),
				TableRow,
				TableHeader,
				CustomTableCell,
				Image.configure({
					inline: true,
				}),
			]),
			"default",
			[
				StarterKit,
				Table.configure({
					resizable: true,
				}),
				TableRow,
				TableHeader,
				CustomTableCell,
				Image.configure({
					inline: true,
				}),
			]
		);
		const documentState = Y.encodeStateAsUpdate(ydoc);
		const base64Encoded = fromUint8Array(documentState);

		const responseData = {
			status: true,
			ydoc: base64Encoded,
		};
		console.log(responseData);
		res.setHeader("Content-Type", "application/json");
		res.json(responseData);
	} catch (error) {
		console.error(error);
		const responseData = {
			status: false,
			message: "An error occured while converting document.",
		};
		console.log(responseData);
		res.setHeader("Content-Type", "application/json");
		res.json(responseData);
	}
});

app.listen(port, () => {
	console.log(`Server is running on port:${port}`);
});

I have also posted this in Discord page, but no help so far. https://discord.com/channels/818568566479257641/818569767149371444

from hocuspocus.

AnnieTJohn avatar AnnieTJohn commented on September 26, 2024

While thanking you for the great help so far, I would like to get an info on the proper method on how to store an initial html converted to ydoc in Rethink or MySQL DB (Means what encoding/decoding we need to do along with TipTap Transformer.

from hocuspocus.

AnnieTJohn avatar AnnieTJohn commented on September 26, 2024

Node server gets the html as request. Then it converts it and sends back to Laravel as response:

const html = req.body.template_content;
const ydoc = TiptapTransformer.toYdoc(
  generateJSON(html, [StarterKit]),
  "default",
  [StarterKit]);
const documentState = Y.encodeStateAsUpdate(ydoc);
const base64Encoded = fromUint8Array(documentState);

When the editor is opened, it fetches this base64Encoded in onLoadDocument and:

const ydoc = toUint8Array(base64Encoded);
return ydoc;

But it does not set initial content in editor. What we are missing here?

from hocuspocus.

AnnieTJohn avatar AnnieTJohn commented on September 26, 2024

https://discord.com/channels/818568566479257641/841708462500085810/1110418557835153448

from hocuspocus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.