facebook / lexical

Lexical is an extensible text editor framework that provides excellent reliability, accessibility and performance.
https://lexical.dev
MIT License
19.66k stars 1.67k forks source link

From here I was able to fetch only the text with face. The styles we have here are not coming. How is this possible? #6595

Open zobkazi opened 2 months ago

zobkazi commented 2 months ago

Discussed in https://github.com/facebook/lexical/discussions/6594

Originally posted by **zobkazi** September 4, 2024 I am using it with Nextjs tsx.. [Github](https://github.com/zbjs/lexical_ant_design_next_tsx) ``` { _id: ObjectId('66d88efc48f48a786e4a683b'), title: 'Name of Organization: Dhaka Ice Cream Industries Limited. (Polar Ice Cream)', slug: 'Position: Assistant Audit Office. (Sales Audit)', content: '{"root":{"children":[{"children":[{"detail":0,"format":1,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"Elevate Your Supply ","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"Chain Career","type":"text","version":1},{"detail":0,"format":3,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":" to the next level","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1},{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":" with the Chartered Supply Chain Professionals™, designa","type":"text","version":1},{"detail":0,"format":16,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"tion program!","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1},{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"Unlock new ","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #7ed321;background-color: #ffffff;","text":"opportunities and el","type":"text","version":1},{"detail":0,"format":1,"mode":"normal","style":"color: #7ed321;background-color: #ffffff;","text":"evate you","type":"text","version":1},{"detail":0,"format":1,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"r","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1},{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":" expertise wi","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #ff3636;background-color: #ffffff;","text":"th Chartered Supply Ch","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"ain P","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #fd3f3f;","text":"rofessionals title to your name! 🌟","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1},{"children":[],"direction":null,"format":"","indent":0,"type":"paragraph","version":1},{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"Are you ready to take your c","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;font-size: 12px;","text":"areer i","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"n supply chain ma","type":"text","version":1},{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"nagement to new heig","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"link","version":1,"rel":"noopener","target":null,"title":null,"url":"https://Position: Assistant Audit Office. (Sales Audit)"},{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"hts?","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"heading","version":1,"tag":"h2"},{"children":[],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1},{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;font-family: Times New Roman;","text":" Lo","type":"text","version":1},{"detail":0,"format":32,"mode":"normal","style":"color: #000000;background-color: #ffffff;font-family: Times New Roman;","text":"ok no furt","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;font-family: Times New Roman;","text":"her! 🚀 Introducing our exclusive \\"Chartered Supply Chain Profes","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"sionals™,\\" ","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1},{"children":[],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1},{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":"designation program - the key to unlocking unparalleled","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1},{"children":[{"detail":0,"format":0,"mode":"normal","style":"color: #000000;background-color: #ffffff;","text":" opportunities in the dyn","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"color: #50e3c2;background-color: #ffffff;","text":"amic world of supply chain!","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}', createdAt: ISODate('2024-09-04T16:46:52.508Z'), updatedAt: ISODate('2024-09-04T16:46:52.508Z'), __v: #0 } ```
etrepum commented 1 month ago

There's not enough information in this issue to help you. There are no screenshots or screen recordings, and no instructions to reproduce whatever problem you're experiencing.

zobkazi commented 1 month ago

Problem Videos::

zobaidulkazi_rich_text_editro_not_fech_data.webm

nextjs: tsx >>

Utility Function

// utils/jsonToHtml.ts
export function jsonToHtml(node: any): string {
  if (!node || typeof node !== 'object') return '';

  const { children, style = '', type = 'div', text } = node;
  const styles = style ? style.replace(/;/g, '; ').trim() : '';

  let html = `<${type} style="${styles}">${text || ''}`;

  if (children) {
    html += children.map((child: any) => jsonToHtml(child)).join('');
  }

  html += `</${type}>`;
  return html;
}

Next.js page Component:

// src/app/news/[slug]/page.tsx
import { useEffect, useState } from 'react';
import { useParams } from 'next/navigation';
import Link from 'next/link';
import { jsonToHtml } from '@/utils/jsonToHtml'; // Adjust the import path as necessary

interface NewsItem {
  title: string;
  slug: string;
  content: string;
}

export default function NewsDetails() {
  const { slug } = useParams();
  const [newsItem, setNewsItem] = useState<NewsItem | null>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    async function fetchNewsItem() {
      try {
        const response = await fetch(`/api/news/${slug}`);
        if (!response.ok) {
          throw new Error('Network response was not ok');
        }
        const data = await response.json();

        // Fix double-escaped JSON
        const fixedContent = JSON.parse(data.content.replace(/\\"/g, '"'));
        data.content = fixedContent;

        setNewsItem(data);
      } catch (error) {
        console.error('Error fetching news item:', error);
      } finally {
        setLoading(false);
      }
    }
    fetchNewsItem();
  }, [slug]);

  if (loading) {
    return <div>Loading...</div>;
  }

  if (!newsItem) {
    return <div>News not found</div>;
  }

  // Convert JSON content to HTML
  const contentHtml = jsonToHtml(newsItem.content);

  return (
    <div>
      <h1>{newsItem.title}</h1>
      <div
        className="prose"
        dangerouslySetInnerHTML={{ __html: contentHtml }}
      />
      <Link href="/news">Back to news list</Link>
    </div>
  );
}

I have a Next.js application where I need to display styled content fetched from a database. The content is stored as a JSON string, which includes various text elements with inline styles such as colors, fonts, and backgrounds. However, the challenge is that while the title and slug of the content display correctly, the JSON content is not rendering with the intended styles. The content appears as raw text without the proper formatting.

etrepum commented 1 month ago

I don't see any Lexical code at all in the code you've pasted. It looks like you are trying to convert the JSON to HTML yourself, instead of using Lexical to generate the HTML. Your video is too long and doesn't clearly demonstrate a problem related to Lexical, it just looks like you're working on something that isn't finished.

zobkazi commented 1 month ago

Our content is in json format, from this format we want to show the data, there is no problem about lexical, since everything is coming right and all the styles and elements are saved by me, so there is no question about it?

Just fetch the data face and show me convert from json to html, that's all it can help, nothing else is needed, since I have everything fine, only I can't fetch the data!

image

Our data is coming like this means we are not only able to convert, RAW data is coming meanwhile can you share any resource or how to convert and show page...

etrepum commented 1 month ago

https://lexical.dev/docs/concepts/serialization

https://lexical.dev/docs/packages/lexical-html

https://lexical.dev/docs/packages/lexical-clipboard

https://lexical.dev/docs/packages/lexical-headless

zobkazi commented 1 month ago

Thanks for sharing the resource, now I try to see if I can go ahead! If I can then I am successful, if not I will definitely take your help, thank you very much