npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@custom-wang-element/core

v1.1.0

Published

一个快速定义wangEditor自定义element的库

Downloads

4

Readme

简介

custom-wang-element是一个快速定义wangEditor自定义element的库,外部组件只要实现ICmp接口,既可以在对应的mvvm框架中接入各自的组件。

安装

custom-wang-element依赖wangEditor,需要提前安装wangEditor,已安装可以跳过

npm install @wangeditor/editor --save

然后安装custom-wang-element

npm install @custom-wang-element/core --save

使用

在初始化wangEditor前,使用createCustomElement创建一个module并在注册:

import { createCustomElement } from "@custom-wang-element/core";
import { Boot } from "@wangeditor/editor";

// 注册一个组件
Boot.registerModule(createCustomElement('countbtn', countBtnCreator))

上面的例子中,'countbtn'是我们创建自定义元素的tag,需要全局唯一,不可重复注册,countBtnCreator是一个CmpCreator函数,CmpCreator函数是一个ICmp的构建函数,countBtnCreator的作用就是构建一个countBtn组件,提供给wangEditor使用。代码大致如下,省略具体实现:

function countBtnCreator(options: CmpCreatorOptions): ICmp {
  let wrapper = document.createElement("div")
  return {
    getEl() {
      return wrapper
    },
    update(options) {
    },
    unmount() {
      wrapper = null
    }
  }
}

注册完组件后,即可在wangEditor中使用,正常我们是在自定义菜单中插入一个自定义元素:

import { customWangElement } from "@custom-wang-element/core";
import { Boot } from "@wangeditor/editor";
class CountBtnMenu {
  constructor() {
    this.title = '计数器'
    this.tag = 'button'
  }
  getValue(editor) {
    return false
  }
  isActive(editor) {
    return true
  }
  isDisabled(editor) {
    return editor.isDisabled()
  };
  exec(editor, value) {
    // 这里插入我们的自定元素,这里我们提供了customWangElement函数来创建一个SlateElement
    editor.insertNode(customWangElement('countbtn', '0'))
  };
  
}

Boot.registerMenu({
  key: 'countbtn-menu', // 定义 menu key :要保证唯一、不重复(重要)
  factory() {
    return new CountBtnMenu() // 把 `YourMenuClass` 替换为你菜单的 class
  },
})

在上面代码中,我们在exec方法中插入一个SlateElement来添加我们的自定义组件。由于我们的自定义元素的SlateElement类型是CustomWangElement,我们提供了customWangElement来快速创建:

import { customWangElement } from "@custom-wang-element/core";

customWangElement('countbtn', '0')

具体的使用,可以查看案例

内置api和接口类型

createCustomElement

function createCustomElement(tag: string, cmpCreator: CmpCreator, options?: Partial<ElemOption>): CustomElementModule

tagwangEditor的组件名,必须唯一

cmpCreator是外部组件构造器,返回一个ICmp对象

options是自定义初始化wangEditor组件根节点snabbdom的vnode参数

只需实现CmpCreator,即可在其他mvvm框架中,接入其自定义组件。

CmpCreator

type CmpCreator = (options: CmpCreatorOptions) => ICmp

CmpCreatorOptions

export type CmpCreatorOptions = CmpUpdateOptions & {
  defaultValue: string,
  updateValue: (arg: string) => void
}

CmpUpdateOptions

export type CmpUpdateOptions = {
  disabled: boolean
  selected: boolean
}

ICmp

interface ICmp {
  getEl(): (Element | null)
  unmount(): void
  update(options: CmpUpdateOptions): void
}

ICmp.getEl是用户获取插入组件的HTMLElement节点。

ICmp.unmount是自定义组件在wangEditor卸载该节点时的钩子函数。

ICmp.update是在编辑器disabledselected更新的时候,通知外部组件更新状态的钩子。

ElemOption

import { VNodeData } from 'snabbdom'

type ElemOption = {
  tag: string
  inline: boolean
  props: Record<string, any>,
} & VNodeData

tag是生成snabbdomVnode是的根节点标签,默认是'span'

inline说明只定义节点是行内元素,而非块元素,默认是true

props是传入snabbdomVnode根节点的props数据,默认是{ contentEditable: false }

VNodeData请查看snabbdom文档

customWangElement

创建一个SlateElement,作用是创建一个createCustomElement注册后的Slate节点

function customWangElement(type: string, value: any): CustomWangElement

第一个参数type是调用createCustomElement的tag,也就是自定义节点的tag

第二个参数value是自定义元素的初始数据,所有非string类型数据,都会被JSON.stringify处理。nullundefined会转成空字符串''

其他mvvm框架的绑定

虽然custom-wang-element提供了抽象的接口,可以自行接入其他mvvm框架中,但是需要自己实现CmpCreator,确实有点麻烦,这里我们提供了如下框架的版本的CmpCreator绑定: