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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@starzhuimeng/formula-editor

v1.0.1

Published

A configurable formula editor with customizable symbols

Downloads

40

Readme

Formula Editor

一个基于TypeScript的数学公式编辑器,支持公式编辑、验证和展示功能。

功能特点

  • 直观的公式编辑器界面
  • 支持各种数学符号和函数
  • 自定义符号配置
  • 公式验证功能
  • 可自定义样式

安装

npm install formula-editor

基本使用

import { FormulaEditor } from 'formula-editor';

// 创建编辑器实例
const editor = new FormulaEditor({
  container: document.getElementById('editor-container')
});

配置选项

const editor = new FormulaEditor({
  container: document.getElementById('editor-container'),
  initialFormula: 'a+b=c',
  readOnly: false,
  symbols: {
    basic: ['+', '-', '×', '÷', '='],
    greek: ['α', 'β', 'γ']
  },
  styles: {
    fontSize: '16px',
    color: '#333',
    backgroundColor: '#fff'
  },
  validation: {
    autoValidate: true,
    rules: [
      { type: 'bracketsMatch', message: '括号不匹配' },
      { type: 'operatorsSurrounded', message: '运算符两侧必须有操作数' }
    ]
  }
});

API参考

基本操作

// 设置公式
editor.setFormula('x^2+y^2=z^2');

// 获取公式
const formula = editor.getFormula();

// 清空编辑器
editor.clear();

// 设置只读模式
editor.setReadOnly(true);

元素操作API

// 获取所有公式元素
const elements = editor.getFormulaElements();

// 获取指定位置的元素
const element = editor.getElementAt(2);

// 在指定位置插入元素
editor.insertElement({
  type: 'number',
  value: '42'
}, 3);

// 更新指定位置的元素
editor.updateElement(1, {
  value: '×',
  type: 'operator'
});

// 删除指定位置的元素
editor.removeElement(4);

// 添加元素到末尾
editor.appendElement({
  type: 'variable',
  value: 'x'
});

// 查找符合条件的元素
const operators = editor.findElements((el) => el.type === 'operator');

// 设置焦点到指定位置
editor.setFocusToIndex(2);

// 获取当前焦点位置
const focusIndex = editor.getCurrentFocusIndex();

// 批量设置元素
editor.setElements([
  { type: 'variable', value: 'a' },
  { type: 'operator', value: '+' },
  { type: 'variable', value: 'b' }
]);

// 复制元素到剪贴板
editor.copyElement(3);

// 获取公式的纯文本表示
const text = editor.getFormulaText();

// 删除当前选中的元素
editor.deleteCurrentElement();

验证相关

// 验证公式
const result = editor.validateFormula();

// 获取最近一次验证结果
const lastResult = editor.getLastValidationResult();

// 设置验证规则
editor.setValidationRules([
  { type: 'bracketsMatch', message: '括号不匹配' }
]);

// 设置自动验证
editor.setAutoValidate(true);

// 设置自定义验证器
editor.setCustomValidator((formula, elements) => {
  // 自定义验证逻辑
  return {
    valid: true,
    errors: []
  };
});

事件监听

// 添加变更事件监听
editor.addEventListener('change', (formula) => {
  console.log('Formula changed:', formula);
});

// 添加验证错误事件监听
editor.addEventListener('validationError', (errors) => {
  console.log('Validation errors:', errors);
});

// 移除事件监听
editor.removeEventListener('change', callback);

自定义符号

const editor = new FormulaEditor({
  container: document.getElementById('editor-container'),
  symbols: {
    basic: [
      { value: '+', type: 'operator', description: '加法' },
      { value: '-', type: 'operator', description: '减法' }
    ],
    customGroup: {
      name: 'customGroup',
      displayName: '自定义符号',
      symbols: [
        { value: '∑', type: 'symbol', description: '求和' },
        { value: '∏', type: 'symbol', description: '求积' }
      ],
      style: {
        backgroundColor: '#f0f0f0',
        color: '#333',
        fontWeight: 'bold'
      }
    }
  }
});

许可证

MIT