feat: 优化截图处理类

This commit is contained in:
ningmengchongshui 2024-06-10 10:05:51 +08:00
parent 8679aec19a
commit 6abe3bb5b1
4 changed files with 37 additions and 4 deletions

7
example/apps.ts Normal file
View File

@ -0,0 +1,7 @@
import { Messages } from '../src/core/index.js'
const message = new Messages({
priority: 9000
})
message.response(/^你好/, async e => {
e.reply('你好')
})

25
example/index.tsx Normal file
View File

@ -0,0 +1,25 @@
import React from 'react'
import { Component, Puppeteer } from '../src/utils/index.js'
import Hello from './hello.tsx'
const Com = new Component()
const Pup = new Puppeteer()
export class Image {
constructor() {
// 启动
Pup.start()
}
/**
*
* @returns
*/
async getHelloComponent() {
// 生成 html 地址
const Address = Com.create(<Hello data={{ name: 'word' }} />, {
html_head: `<link rel="stylesheet" href="../public/output.css"></link>`,
html_name: 'hello.html'
})
//
const img = await Pup.render(Address)
return img
}
}

View File

@ -31,12 +31,13 @@ export class Component {
*/ */
create(element: React.ReactNode, options: ComponentCreateOpsionType) { create(element: React.ReactNode, options: ComponentCreateOpsionType) {
const str = renderToString(element) const str = renderToString(element)
const dir = join(this.#dir, options.join_dir) const dir = join(this.#dir, options?.join_dir ?? '')
mkdirSync(dir, { recursive: true }) mkdirSync(dir, { recursive: true })
const address = join(dir, options.html_name) const address = join(dir, options?.html_name ?? 'hello.html')
const DOCTYPE = '<!DOCTYPE html>' const DOCTYPE = '<!DOCTYPE html>'
const head = `<head>${options.html_head}</head>` const Link = `<link rel="stylesheet" href="../public/output.css"></link>`
const body = `<body>${str}${options.html_body}</body>` const head = `<head>${options?.html_head ?? Link}</head>`
const body = `<body> ${str} ${options?.html_body ?? ''}</body>`
const html = `${DOCTYPE}<html>${head}${body}</html>` const html = `${DOCTYPE}<html>${head}${body}</html>`
writeFileSync(address, html) writeFileSync(address, html)
return address return address