toi cam thay minh la sieu nhan

This commit is contained in:
2024-11-15 18:18:37 +07:00
parent ff45e69efd
commit 7e4a4283e6
12 changed files with 499 additions and 12 deletions

View File

@@ -0,0 +1,8 @@
export enum DocumentEvent {
CREATED = 'document_created',
CHANGED = 'document_changed',
DELETED = 'document_deleted',
SAVED = 'document_saved',
PAGE_CREATED = 'document_page_created',
PAGE_DELETED = 'document_page_deleted',
}

View File

@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common'
import { DocumentService } from './document.service'
import { DocumentSchema } from './document.schema'
import { JSDOM } from 'jsdom'
@Module({
providers: [JSDOM, DocumentService, DocumentSchema],
exports: [DocumentService, DocumentSchema],
})
export class DocumentModule {}

View File

@@ -0,0 +1,110 @@
import { Inject, Injectable, Logger } from '@nestjs/common'
import {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos'
import { Builder, SchemaContext } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { DocumentEvent } from './document.event'
import { Document } from '@prisma/client'
@Injectable()
export class DocumentSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
) {
super()
}
@PothosRef()
document() {
return this.builder.prismaObject('Document', {
fields: (t) => ({
id: t.exposeID('id'),
name: t.exposeString('name'),
fileUrl: t.exposeString('fileUrl'),
createdAt: t.expose('createdAt', { type: 'DateTime' }),
updatedAt: t.expose('updatedAt', { type: 'DateTime' }),
owner: t.relation('owner'),
ownerId: t.exposeID('ownerId'),
collaborators: t.exposeStringList('collaborators'),
isPublic: t.exposeBoolean('isPublic'),
}),
})
}
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
document: t.prismaField({
type: this.document(),
args: this.builder.generator.findUniqueArgs('Document'),
resolve: async (query, _root, args) => {
return await this.prisma.document.findUnique({
...query,
where: args.where,
})
},
}),
documents: t.prismaField({
type: [this.document()],
args: this.builder.generator.findManyArgs('Document'),
resolve: async (query, _root, args) => {
return await this.prisma.document.findMany({
...query,
skip: args.skip ?? undefined,
take: args.take ?? undefined,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
})
},
}),
}))
this.builder.mutationFields((t) => ({
createDocument: t.prismaField({
type: this.document(),
args: {
data: t.arg({
type: this.builder.generator.getCreateInput('Document'),
required: true,
}),
},
resolve: async (query, _root, args) => {
return await this.prisma.document.create({
...query,
data: args.data,
})
},
}),
}))
this.builder.subscriptionFields((t) => ({
document: t.field({
type: this.document(),
args: {
documentId: t.arg({
type: 'String',
required: true,
}),
},
subscribe: (_, args, ctx: SchemaContext) => {
if (!ctx.isSubscription) throw new Error('Not allowed')
const {
websocket: { pubSub },
} = ctx
return pubSub.asyncIterator([
`${DocumentEvent.CHANGED}.${args.documentId}`,
`${DocumentEvent.CREATED}.${args.documentId}`,
`${DocumentEvent.DELETED}.${args.documentId}`,
`${DocumentEvent.SAVED}.${args.documentId}`,
]) as unknown as AsyncIterable<Document>
},
resolve: async (payload: Document) => payload,
}),
}))
}
}

View File

@@ -0,0 +1,42 @@
import { forwardRef, Inject, Injectable } from '@nestjs/common'
import Delta, { Op } from 'quill-delta'
import { MinioService } from '../Minio/minio.service'
import { DocumentDelta } from './document.type'
import { JSDOM } from 'jsdom'
@Injectable()
export class DocumentService {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
private quill: any
constructor(
private readonly minio: MinioService,
public document: JSDOM,
) {
;(async () => {
await this.loadQuill()
})()
}
private async loadQuill() {
if (typeof window === 'undefined' || typeof document === 'undefined') {
const { window } = new JSDOM('<!doctype html><html><body></body></html>')
const { navigator } = window
const { Node } = window
global.window = window
global.document = window.document
global.navigator = navigator
global.Node = Node
}
const { default: Quill } = await import('quill')
this.quill = Quill
}
async handleOnChange(delta: DocumentDelta) {}
async handleOnSave() {}
async handleOnSync() {}
async requestSync() {}
}

View File

@@ -0,0 +1,6 @@
import Delta from 'quill-delta'
export type DocumentDelta = Delta & {
pageIndex: number
documentId: string
}