0.11.3 • Published 2 years ago

jcode-tools v0.11.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

JCode Tools

简介

这是为码上掘金提供的JS工具库,用它来增强码上掘金的能力和弥补它的不足。

API

1. JCode.getCustomCode

自定义Script模式下,获得代码文本。

const code = await JCode.getCustomCode(); 

2. JCode.logger

将控制台内容输出到页面上。

const logger = JCode.logger(container);
logger.log('Hello %cworld', 'color:red');

3. JCode.CodeXClient

运行服务端代码。

支持同步和异步模式。

同步模式:在线demo

<script type="module">
import {CodeXClient, logger} from 'https://cdn.jsdelivr.net/npm/jcode-tools/dist/jcode-tools.esm.js';
(async () => {
  window.console = logger(app);
  console.log('提交代码,请稍候');

  try {
    const code = new CodeXClient(); // 默认会从CustomCode中获取
    const result = await code.runCode({input: ['1 1']});
    if(result.error) {
      console.error(result.error);
    }
    console.log(result.output);
  } catch(ex) {
    console.error(ex);
  }
})();
</script>

异步模式:在线demo

<script type="module">
import {CodeXClient, logger} from 'https://cdn.jsdelivr.net/npm/jcode-tools/dist/jcode-tools.esm.js';
(async () => {
  window.console = logger(app);
  console.log('提交代码,请稍候');

  try {
    const code = new CodeXClient();
    code.onmessage = (msg) => {
      console.log(msg);
    }
    code.onerror = (msg) => {
      console.error(msg);
    }

    const result = await Promise.all(
      [code.runCode()
      , code.input('1 1')
      , code.input('2 3')
      , code.input('')]);
    if(result[0].error) {
      console.error(result[0].error);
    }
  } catch(ex) {
    console.error(ex);
  }
})();
</script>

4. KV存储服务

提供简单的KV存储,可以存储一些数据到服务器。

import {Storage} from 'https://cdn.jsdelivr.net/npm/jcode-tools/dist/jcode-tools.esm.js';

const storage = new Storage();
const successful = await storage.set('juejin', {id: 'value'});
if(successful) {
  const data = await storage.get('juejin');
  console.log(data); // {id: 'value'}
  await storage.del('juejin');
} else {
  console.error(storage.result.error);
}

使用方式

  1. 可以直接通过 CDN 引入 https://cdn.jsdelivr.net/npm/jcode-tools 以及对应的 css https://cdn.jsdelivr.net/npm/jcode-tools/dist/jcode-tools.css;
  1. 可以通过 ESModule 引入:
import {CodeXClient, logger} from 'https://cdn.jsdelivr.net/npm/jcode-tools/dist/jcode-tools.esm.js';

如何贡献代码

Clone 本项目,npm install 安装依赖,然后运行 npm run dev

设置 http://localhost:8000/dist/jcode-tools.js 到码上掘金项目的依赖资源中即可调试运行。

0.11.3

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.5

2 years ago

0.11.0

2 years ago

0.10.1

2 years ago

0.10.2

2 years ago

0.10.3

2 years ago

0.10.4

2 years ago

0.10.5

2 years ago

0.10.6

2 years ago

0.10.0

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.1.2

2 years ago

0.2.0

2 years ago

0.9.0

2 years ago

0.8.1

2 years ago

0.7.2

2 years ago

0.8.0

2 years ago

0.7.1

2 years ago

0.6.2

2 years ago

0.4.4

2 years ago

0.9.2

2 years ago

0.8.3

2 years ago

0.9.1

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.3.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.2.2

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.5.2

2 years ago

0.4.3

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.4.2

2 years ago

0.1.0

3 years ago

0.1.1

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago