@calumk/codecup

中文文档
1.9.4发布于 2024/11/24MITCommonJS近 30 天下载 2.7万

A micro code-editor for awesome web pages

原始使用文档
文档目录14 节
  1. Why?
  2. Kitchen Sink Example
  3. Changes
  4. Core Changes as PR
  5. Installation
  6. Usage
  7. Listening for changes in editor
  8. Updating the editor programatically
  9. Getting the current code from editor
  10. Copy Button
  11. Enabling line numbers
  12. Setting max and min lines
  13. Enabling read only mode
  14. changing other languages support:



codecup: A micro code-editor for awesome web pages.
(Originally A Fork of CodeFlask)

Why?

CodeFlask was a brilliant project, but seems to be unmaintained, and it required some significant updates to work with a new project I am working on.

Kitchen Sink Example

cup = new codecup('#cf_holder', { 
    language: "javascript",
    lineNumbers: true ,
    copyButton: true,
    maxLines : 15,
    minLines : 5
});

Changes

  • Added max line number option
  • Switched from rollup to webpack
  • Added example folder
  • disabled e2e tests
  • Small theme tweaks (Border, rounded corners.)
  • updateded all codeflask references to codecup - 13/11/2023

[!IMPORTANT] Languages are now loaded async (via prismjs), so you can load any language you want, without having to bundle them all. - Jan 2024

[!NOTE] Languages supported are : https://prismjs.com/#supported-languages

Core Changes as PR

(Submitted as PR to CodeFlask, incase it gets picked up again)

  • Added support to destroy
  • Added Linenumber add / remove
  • Added Linenumber toggle
  • Added Readonly toggle

Installation

You can install codecup via npm:

npm i @calumk/codecup

Or use it directly in browser via cdn service:

https://cdn.jsdelivr.net/npm/@calumk/codecup

Usage

import codecup from '@calumk/codecup';

const cup = new codecup('#my-selector', { language: 'js' });

You can also pass a DOM element instead of a selector:

import codecup from 'codecup';

const editorElem = document.getElementById('editor');
const cup = new codecup(editorElem, { language: 'js' });

Usage with Shadow DOM:

import codecup from 'codecup';
...
const shadowElem = this.shadowRoot.querySelector('#editor');
const cup = new codecup(shadowElem, { language: 'js', styleParent: this.shadowRoot });

Listening for changes in editor

cup.onUpdate((code) => {
  // do something with code here.
  // this will trigger whenever the code
  // in the editor changes.
});

Updating the editor programatically

// This will also trigger .onUpdate()
cup.updateCode('const my_new_code_here = "Blabla"');

Getting the current code from editor

const code = cup.getCode();

Copy Button

The copy button is now enabled by default, and will copy the code to the clipboard when clicked.

it can be disabled by passing copyButton: false in the options.

const cup = new codecup('#my-selector', {
  language: 'javascript',
  copyButton: false
});

Enabling line numbers

import codecup from 'codecup';

const cup = new codecup('#my-selector', {
  language: 'js',
  lineNumbers: true
});

You can also toggle line numbers after the editor is created:

cup.toggleLineNumbers();

Setting max and min lines

[!IMPORTANT] As of 1.90, You can also set the max line number, (Default is 100), And the min line number, (Default is 1)

[!NOTE] If you want it to be a fixed number of lines, set both to the same number.

import codecup from 'codecup';

const cup = new codecup('#my-selector', {
  language: 'js',
  lineNumbers: true,
  maxLines: 10,
  minLines: 10
});

Enabling read only mode

import codecup from 'codecup';

const cup = new codecup('#my-selector', {
  language: 'javascript',
  readonly: true
});

changing other languages support:

cup.updateLanguage('ruby')
// 
cup.updateLanguage('javascript')
//

For Example to add change dynamically from 'Ruby' to 'javascript'

import Prism from 'prismjs';
import codecup from 'codecup';

const cup = new codecup('#my-selector', {
  language: 'ruby',
  readonly: true
});

cup.updateLanguage('javascript');

版本与发布记录

@calumk/codecup 共发布 15 个版本,首次发布于 2022/05/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.9.4latest2024/11/242 年前
1.9.32024/11/242 年前
1.9.22024/01/292 年前
1.9.12024/01/282 年前
1.9.02024/01/282 年前
1.8.12024/01/252 年前

关于 @calumk/codecup 的常见问题

@calumk/codecup 是什么?

A micro code-editor for awesome web pages

如何安装 @calumk/codecup?

在项目目录运行 npm install @calumk/codecup,或使用 pnpm add / yarn add / bun add。

@calumk/codecup 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/calumk__codecup 是否存在。

@calumk/codecup 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@calumk/codecup 可以商用吗?

@calumk/codecup 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@calumk/codecup 的下载量有多少?

根据 npm 官方下载统计,@calumk/codecup 在 2026/10/09 – 2026/11/07 这 30 天内被下载 27,348 次,日均约 912 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@calumk/codecup 还在维护吗?

最新版本 1.9.4 发布于 2024/11/24(2 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/05/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。