nurs/editor.js

Editor.js的PHP后端实现

dev-master 2023-12-22 17:14 UTC

This package is auto-updated.

Last update: 2024-09-22 18:39:01 UTC


README

Editor.js的服务器端实现示例。它包含数据验证、HTML清理并将Editor.js的输出转换为Block对象。

安装

使用composer安装库

composer require nurs/editor.js:dev-master

指南

在您的PHP脚本顶部添加此行

use \EditorJS\EditorJS;

此行允许您获取具有以下方法的编辑器类:

getBlocks - 返回经过清理的块数组

基本用法

您可以从编辑器获取数据并将其作为参数发送到编辑器的服务器验证器,如下所示:

try {
    // Initialize Editor backend and validate structure
    $editor = new EditorJS( $data, $configuration );
    
    // Get sanitized blocks (according to the rules from configuration)
    $blocks = $editor->getBlocks();
    
} catch (\EditorJSException $e) {
    // process exception
}

Editor.js构造函数具有以下参数

$data — 来自CodeX Editor前端的数据的JSON字符串。

$configuration — CodeX Editor工具配置的JSON字符串(见下文示例)。

添加了新的块过滤方法

  $blocks = $editor->getFilteringBlocks(['header','image']);    

配置文件

您可以手动配置不同类型Editor.js工具(标题、段落、列表、引用等)的验证规则。您还可以通过添加新工具来扩展配置。

示例验证规则集

{
  "tools": {
    "header": {
      "text": {
        "type": "string",
        "required": true,
        "allowedTags": "b,i,a[href]"
      },
      "level": {
        "type": "int",
        "canBeOnly": [2, 3, 4]
      }
    }
  }
}

其中

tools — 支持的Editor.js工具数组。

header — 定义header工具设置。

textlevelheader工具结构中的参数。

text 是一个必需字符串,除bia[href] 标签外将被清理。

level 是一个可选整数,可以是0、1或2。

allowedTags 参数应遵循HTMLPurifier格式。

每个块有三个常见的参数

  1. type (必需) — 块类型
  1. allowedTags (可选) — 不会删除的字符串中的HTML标签

其他值可根据HTMLPurifier格式使用。

示例

"paragraph": {
    "text": {
        "type": "string",
        "allowedTags": "i,b,u,a[href]"
    }
}
  1. canBeOnly (可选) — 定义允许值集合

示例

"quote": {
      "text": {
        "type": "string"
      },
      "caption": {
        "type": "string"
      },
      "alignment": {
        "type": "string",
        "canBeOnly": ["left", "center"]
      }
    }

简写设置语法

添加了一些语法糖。

工具设置可以是string。它定义了具有默认设置的工具体型。

"header": {
  "text": "string",
  "level": "int"
}

它计算为

"header": {
  "text": {
    "type": "string",
    "allowedTags": "",
    "required": true
  },
  "level": {
    "type": "int",
    "allowedTags": "",
    "required": true
  }
}

工具设置可以是array。它定义了一组未经清理的允许值。

"quote": {
  "alignment": ["left", "center"],
  "caption": "string"
}

它计算为

"quote": {
  "alignment": {
    "type": "string",
    "canBeOnly": ["left", "center"]
  },
  "caption": {
      "type": "string",
      "allowedTags": "",
      "required": true
    }
}

另一个配置示例:/tests/samples/syntax-sugar.json

嵌套工具

工具可以包含嵌套值。这是通过array类型实现的。

假设JSON输入如下:

{
    "blocks": [
        "type": list,
        "data": {
            "items": [
                "first", "second", "third"
            ],
            "style": {
                "background-color": "red",
                "font-color": "black"
            }
        }
    ]
}

我们可以在配置中为该输入定义验证规则

"list": {
  "items": {
    "type": "array",
    "data": {
      "-": {
        "type": "string",
        "allowedTags": "i,b,u"
      }
    }
  },
  "style": {
      "type": "array",
      "data": {
        "background-color": {
            "type": "string",
            "canBeOnly": ["red", "blue", "green"]
        },
        "font-color": {
            "type": "string",
            "canBeOnly": ["black", "white"]
        }
      }
  }
}

其中data是数组值的容器,而-是数组为顺序时的特殊快捷方式。

另一个配置示例:/tests/samples/test-config.json

异常

EditorJS类

BlockHandler类

ConfigLoader类

制作工具

如果您在前端连接了新的工具,那么您应该为该工具创建一个配置规则,以便在服务器端验证它。

仓库

https://github.com/indexcoder/editorjs-php

关于我

我们是一支由吉尔吉斯斯坦伊塞克阿勒泰的学生和毕业生组成的Web开发爱好者小队。请随时通过programmer.kg@mail.ru给我们反馈。