Code Monkey home page Code Monkey logo

haixin-fang / tojson.js Goto Github PK

View Code? Open in Web Editor NEW
33.0 1.0 3.0 3.61 MB

tojson.js is a class library that supports parsing Psd, Sketch, and PPT to JSON. This JSON conforms to the data format of fabric.js rendering. (tojson.js 是一个支持解析 Psd、Sketch、PPT转 json 的类库, 该 json 满足 fabric.js 渲染的数据格式)

Home Page: https://haixin-fang.github.io/tojson.js/playground/

JavaScript 84.72% Shell 0.12% HTML 2.42% Vue 11.75% CSS 0.98%
ai pdf photoshop ppt pptx psd sketch vite vue3

tojson.js's Introduction

tojson.js

tojson.js 是一个支持解析 Psd、Sketch、PPTX 转 json 的类库, 该 json 满足 fabric.js 渲染的数据格式.

在线体验

https://haixin-fang.github.io/tojson.js/playground

环境准备

playground 的示例项目,就是为开发者提供的基础应用示例。

node.js >= 16

先安装 pnpm

$ npm install -g pnpm

然后安装依赖

$ pnpm bootstrap

运行项目

执行命令

$ pnpm dev

最后在浏览器中打开

即可得到一个解析 Sketch, Psd 为 json 的项目

项目介绍

在本项目中,我们核心内容,是包含在 packages/* , 其中是解析各个文件的核心代码

  • psd-json.js 解析 psd 转 json 的类库
  • sketchtojson 解析 sketch 转 json 的类库
  • pptx-json 解析 pptx 转 json 的类库
  • tojson.js 解析 psd、sketch、pptx 转 json

使用

安装

npm i -S tojson.js

npm i -S psd-json.js

npm i -S sketchtojson

使用

import toJson, { getFileType, types as fileTypes, psdtojson, sketchtojson, pptxtojson } from "tojson.js";
const options = {
  uploadUrl: options.uploadUrl, // 图片上传的接口地址
  uploadCallback: options.uploadCallback, // 上传完成后的数据处理回调方法
};
// psd、sketch文件
const result = await toJson(files, options);
/**
 * 创建实例和解析文件分开,可以在一个实例中解析 n 个文件
 */
// 或
const instance = new pptxtojson(options);
const result = instance.init(files);
// 或
const instance = new sketchtojson(options);
const result = instance.init(files);
// 或
const instance = new pptxtojson(options);
const result = instance.init(files);
  • getFileType 方法能获取文件类型
  • types 返回支持的文件类型, 如 psd、sketch

sketch 文件解析

import sketchtojson from "sketchtojson";
const options = {
  uploadUrl: options.uploadUrl, // 图片上传的接口地址
  uploadCallback: options.uploadCallback, // 上传完成后的数据处理回调方法
};
const instance = new sketchtojson(options);
const result = instance.init(files);

psd 文件解析

import psdtojson from "psd-json.js";
const options = {
  uploadUrl: options.uploadUrl, // 图片上传的接口地址
  uploadCallback: options.uploadCallback, // 上传完成后的数据处理回调方法
};
const instance = new pptxtojson(options);
const result = instance.init(files);

pptx 文件解析

import pptxtojson from "pptx-json";
const options = {
  uploadUrl: options.uploadUrl, // 图片上传的接口地址
  uploadCallback: options.uploadCallback, // 上传完成后的数据处理回调方法
};
const instance = new pptxtojson(options);
const result = instance.init(files);

未来

后续会支持的功能

  • pdf 解析

tojson.js's People

Contributors

haixin-fang avatar tclxhaixin-fang avatar

Stargazers

 avatar 毕晨曦 avatar 麻辣石头 avatar KapK avatar Bhaskar avatar 大轰 avatar wjx0820 avatar Charles Lo avatar SeongjaeMoon avatar Frank Cheung avatar 拾光, avatar totorofly avatar osmen avatar  avatar  avatar  avatar Kernel avatar Paul P. Pope avatar  avatar withoutRock avatar mozhu avatar WCHENG avatar 屋良 avatar 秦少卫   avatar MRNAN avatar Tetap avatar 公子 avatar fenggj avatar  avatar jhs1873 avatar _island avatar morestrive avatar  avatar

Watchers

 avatar

tojson.js's Issues

bug: pptx自定义形状解析

定位到错误:
/pptx-json/src/index.js

不能正确解析常规形状之外的图形:

line 750

  if (custShapType && type !== "diagram") {
    const ext = getTextByPathList(slideXfrmNode, ["a:ext", "attrs"]);
    const cx = parseInt(ext["cx"]) * SLIDE_FACTOR;
    const cy = parseInt(ext["cy"]) * SLIDE_FACTOR;
    const w = parseInt(ext["cx"]) * SLIDE_FACTOR;
    const h = parseInt(ext["cy"]) * SLIDE_FACTOR;
    // TypeError: Cannot read properties of undefined (reading 'w')
    let d = '';
    console.log(custShapType, w, h)
    try {
      d = getCustomShapePath(custShapType, w, h);

    } catch (e) {
      console.log(e, 'Error')
      d = '';
    }
    return {
      ...data,
      type: "shape",
      cx,
      cy,
      shapType: "custom",
      path: d,
    };
  }

shape.js

这里忽略了数组这种情况

export function getCustomShapePath(custShapType, w, h) {
  const pathLstNode = getTextByPathList(custShapType, ['a:pathLst'])
  let pathNodes = getTextByPathList(pathLstNode, ['a:path'])

  if (Array.isArray(pathNodes)) {
    pathNodes = pathNodes.pop()
  }
  const maxX = parseInt(pathNodes['attrs']['w'])
  const maxY = parseInt(pathNodes['attrs']['h'])
  ....
  ...
}

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.