✨(frontend) initial editor content is now a heading
When we create a new document, the initial content is now a heading instead of a paragraph. This is to make it easier to set the title of the document.
This commit is contained in:
@@ -6,6 +6,8 @@ import { create } from 'zustand';
|
|||||||
import { providerUrl } from '@/core';
|
import { providerUrl } from '@/core';
|
||||||
import { Base64 } from '@/features/docs/doc-management';
|
import { Base64 } from '@/features/docs/doc-management';
|
||||||
|
|
||||||
|
import { blocksToYDoc } from '../utils';
|
||||||
|
|
||||||
interface DocStore {
|
interface DocStore {
|
||||||
provider: HocuspocusProvider;
|
provider: HocuspocusProvider;
|
||||||
editor?: BlockNoteEditor;
|
editor?: BlockNoteEditor;
|
||||||
@@ -28,6 +30,15 @@ export const useDocStore = create<UseDocStore>((set, get) => ({
|
|||||||
|
|
||||||
if (initialDoc) {
|
if (initialDoc) {
|
||||||
Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64'));
|
Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64'));
|
||||||
|
} else {
|
||||||
|
const initialDocContent = [
|
||||||
|
{
|
||||||
|
type: 'heading',
|
||||||
|
content: '',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
blocksToYDoc(initialDocContent, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
const provider = new HocuspocusProvider({
|
const provider = new HocuspocusProvider({
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import * as Y from 'yjs';
|
||||||
|
|
||||||
export const randomColor = () => {
|
export const randomColor = () => {
|
||||||
const randomInt = (min: number, max: number) => {
|
const randomInt = (min: number, max: number) => {
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
@@ -26,3 +28,20 @@ function hslToHex(h: number, s: number, l: number) {
|
|||||||
export const toBase64 = (
|
export const toBase64 = (
|
||||||
str: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>,
|
str: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>,
|
||||||
) => Buffer.from(str).toString('base64');
|
) => Buffer.from(str).toString('base64');
|
||||||
|
|
||||||
|
type BasicBlock = {
|
||||||
|
type: string;
|
||||||
|
content: string;
|
||||||
|
};
|
||||||
|
export const blocksToYDoc = (blocks: BasicBlock[], doc: Y.Doc) => {
|
||||||
|
const xmlFragment = doc.getXmlFragment('document-store');
|
||||||
|
|
||||||
|
blocks.forEach((block) => {
|
||||||
|
const xmlElement = new Y.XmlElement(block.type);
|
||||||
|
if (block.content) {
|
||||||
|
xmlElement.insert(0, [new Y.XmlText(block.content)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
xmlFragment.push([xmlElement]);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user