soiz1 commited on
Commit
503af9c
·
verified ·
1 Parent(s): 906f997

Upload folder using huggingface_hub

Browse files
.eslintignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /src/util.js
.eslintrc.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "env": {
3
+ "browser": true,
4
+ "es6": true,
5
+ "amd": true,
6
+ "commonjs": true
7
+ },
8
+ "parserOptions": {
9
+ "sourceType": "module"
10
+ },
11
+ "extends": "eslint:recommended",
12
+ "rules": {
13
+ "indent": ["error", 2],
14
+ "quotes": ["error", "single"],
15
+ "no-extra-semi": "off"
16
+ }
17
+ }
.github/FUNDING.yml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ ko_fi: surunzi
2
+ open_collective: eruda
.github/workflows/main.yml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: CI
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ push:
6
+ branches:
7
+ - 'master'
8
+ paths:
9
+ - 'src/**/*'
10
+
11
+ jobs:
12
+ ci:
13
+
14
+ runs-on: ubuntu-latest
15
+
16
+ strategy:
17
+ matrix:
18
+ node-version: [18.x]
19
+
20
+ steps:
21
+ - uses: actions/checkout@v2
22
+ - name: Use Node.js ${{ matrix.node-version }}
23
+ uses: actions/setup-node@v2
24
+ with:
25
+ node-version: ${{ matrix.node-version }}
26
+ - run: |
27
+ npm i
28
+ npm run ci
.github/workflows/publish.yml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Publish to NPM
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ release:
6
+ types: [created]
7
+
8
+ jobs:
9
+ publish:
10
+
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - name: Checkout
15
+ uses: actions/checkout@v2
16
+ - name: Setup Node
17
+ uses: actions/setup-node@v2
18
+ with:
19
+ node-version: '18.x'
20
+ registry-url: 'https://registry.npmjs.org'
21
+ - name: Build eruda code
22
+ run: |
23
+ npm i
24
+ npm run build
25
+ - name: Publish package on NPM
26
+ run: npm publish
27
+ env:
28
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ /node_modules/
2
+ eruda-code.js
3
+ eruda-code.js.map
4
+ package-lock.json
.prettierignore ADDED
@@ -0,0 +1 @@
 
 
1
+ eruda-code.js
.prettierrc.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "singleQuote": true,
3
+ "tabWidth": 2,
4
+ "semi": false
5
+ }
CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## v2.2.0 (22 Jul 2024)
2
+
3
+ * fix: cursor color
4
+
5
+ ## v2.1.0 (29 Nov 2020)
6
+
7
+ * feat: support theme
8
+
9
+ ## v2.0.0 (6 Jan 2020)
10
+
11
+ * feat: theme support
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 liriliri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,10 +1,30 @@
1
- ---
2
- title: Eruda Code
3
- emoji: 🔥
4
- colorFrom: green
5
- colorTo: blue
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # eruda-code
2
+
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![Build status][ci-image]][ci-url]
5
+ [![License][license-image]][npm-url]
6
+
7
+ [npm-image]: https://img.shields.io/npm/v/eruda-code.svg
8
+ [npm-url]: https://npmjs.org/package/eruda-code
9
+ [ci-image]: https://img.shields.io/github/actions/workflow/status/liriliri/eruda-code/main.yml?branch=master&style=flat-square
10
+ [ci-url]: https://github.com/liriliri/eruda-code/actions/workflows/main.yml
11
+ [license-image]: https://img.shields.io/npm/l/eruda-code.svg
12
+
13
+ Eruda plugin for running JavaScript code.
14
+
15
+ ## Demo
16
+
17
+ Browse it on your phone:
18
+ [https://eruda.liriliri.io/?plugin=code](https://eruda.liriliri.io/?plugin=code)
19
+
20
+ ## Install
21
+
22
+ ```bash
23
+ npm install eruda-code --save
24
+ ```
25
+
26
+ ```javascript
27
+ eruda.add(erudaCode);
28
+ ```
29
+
30
+ Make sure Eruda is loaded before this plugin, otherwise won't work.
index.html ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
6
+ <title>Eruda-code</title>
7
+ </head>
8
+ <body>
9
+ <script src="node_modules/eruda/eruda.js"></script>
10
+ <script>eruda.init({tool: 'console'});</script>
11
+ <script src="assets/eruda-code.js"></script>
12
+ <script>eruda.add(erudaCode).show('code').show();</script>
13
+ </body>
14
+ </html>
package.json ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "eruda-code",
3
+ "version": "2.2.0",
4
+ "description": "Eruda plugin for running JavaScript code",
5
+ "main": "eruda-code.js",
6
+ "browserslist": [
7
+ "since 2015",
8
+ "not dead"
9
+ ],
10
+ "files": [
11
+ "eruda-code.js",
12
+ "eruda-code.js.map"
13
+ ],
14
+ "scripts": {
15
+ "dev": "webpack-dev-server --host 0.0.0.0 --mode development",
16
+ "build": "webpack --mode production",
17
+ "ci": "npm run lint && npm run build && npm run es5",
18
+ "lint": "eslint src/**/*.js",
19
+ "format": "lsla prettier \"src/*.{js,scss}\" \"*.{js,json}\" --write",
20
+ "es5": "es-check es5 eruda-code.js"
21
+ },
22
+ "keywords": [
23
+ "eruda",
24
+ "plugin"
25
+ ],
26
+ "author": "surunzi",
27
+ "devDependencies": {
28
+ "@babel/core": "^7.21.3",
29
+ "@babel/plugin-transform-runtime": "^7.21.0",
30
+ "@babel/preset-env": "^7.20.2",
31
+ "autoprefixer": "^10.4.14",
32
+ "babel-loader": "^9.1.2",
33
+ "codemirror": "^5.65.17",
34
+ "css-loader": "^3.4.2",
35
+ "eruda": "^3.2.0",
36
+ "es-check": "^7.2.1",
37
+ "eslint": "^8.57.0",
38
+ "js-beautify": "^1.14.7",
39
+ "licia": "^1.41.0",
40
+ "node-sass": "^8.0.0",
41
+ "postcss": "^8.4.21",
42
+ "postcss-class-prefix": "^0.3.0",
43
+ "postcss-loader": "^7.0.2",
44
+ "sass": "^1.77.8",
45
+ "sass-loader": "^14.2.1",
46
+ "webpack": "^5.93.0",
47
+ "webpack-cli": "^5.1.4",
48
+ "webpack-dev-server": "^4.12.0"
49
+ }
50
+ }
src/CodeMirror.css ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .CodeMirror {
2
+ height: 100%;
3
+ background: var(--background);
4
+ color: var(--foreground);
5
+ }
6
+
7
+ .CodeMirror-cursor {
8
+ border-left: 1px solid var(--light);
9
+ border-right: 1px solid var(--dark);
10
+ }
11
+
12
+ .CodeMirror-gutters {
13
+ background: var(--background);
14
+ border-color: var(--border);
15
+ }
16
+
17
+ /* Sync style with chrome dev-tool */
18
+ .cm-s-default .cm-keyword {
19
+ color: var(--keyword-color);
20
+ }
21
+ .cm-s-default .cm-number {
22
+ color: var(--number-color);
23
+ }
24
+ .cm-s-default .cm-comment {
25
+ color: var(--comment-color) !important;
26
+ font-style: italic;
27
+ }
28
+ .cm-s-default .cm-string,
29
+ .cm-s-default .com-string-2 {
30
+ color: var(--string-color);
31
+ }
32
+ .cm-atom {
33
+ color: var(--attribute-name-color);
34
+ }
35
+ .cm-s-default .cm-def {
36
+ color: var(--foreground);
37
+ }
38
+ .cm-operator {
39
+ color: var(--operator-color);
40
+ }
41
+ .cm-s-default .cm-meta {
42
+ color: var(--tag-name-color);
43
+ }
44
+ .cm-s-default .cm-variable-2 {
45
+ color: var(--var-color);
46
+ }
src/code.js ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import CodeMirror from 'codemirror'
2
+ import 'codemirror/mode/javascript/javascript'
3
+ import nextTick from 'licia/nextTick'
4
+ import copy from 'licia/copy'
5
+ import beautify from 'js-beautify'
6
+ import stripIndent from 'licia/stripIndent'
7
+
8
+ export default function (eruda) {
9
+ let { evalCss } = eruda.util
10
+
11
+ class Code extends eruda.Tool {
12
+ constructor() {
13
+ super()
14
+ this.name = 'code'
15
+ this._style = evalCss(require('./style.scss'))
16
+ this._CodeMirrorStyle = evalCss(
17
+ require('codemirror/lib/codemirror.css') +
18
+ require('codemirror/theme/material-darker.css')
19
+ )
20
+ this._CodeMirrorCustomStyle = evalCss(require('./CodeMirror.css'))
21
+ this._editor = null
22
+ }
23
+ init($el, container) {
24
+ super.init($el, container)
25
+
26
+ $el.html(stripIndent`<textarea class="eruda-editor">function fib(num) {
27
+ if (num <= 2) return 1;
28
+ return fib(num - 1) + fib(num - 2);
29
+ }
30
+ console.log(fib(5));</textarea>
31
+ <div class="eruda-bottom-bar">
32
+ <div class="eruda-btn eruda-beautify">
33
+ Beautify
34
+ </div>
35
+ <div class="eruda-btn eruda-copy">
36
+ Copy
37
+ </div>
38
+ <div class="eruda-btn eruda-clear">
39
+ Clear
40
+ </div>
41
+ <div class="eruda-btn eruda-run">
42
+ Run
43
+ </div>
44
+ </div>`)
45
+ this._bindEvent()
46
+ }
47
+ show() {
48
+ super.show()
49
+ if (!this._editor) {
50
+ let container = this._$el.find('.eruda-editor').get(0)
51
+ this._editor = CodeMirror.fromTextArea(container, {
52
+ lineNumbers: 'true',
53
+ mode: 'javascript',
54
+ })
55
+ nextTick(() => this._editor.refresh())
56
+ }
57
+ }
58
+ hide() {
59
+ super.hide()
60
+ }
61
+ run() {
62
+ try {
63
+ evalJs(this._editor.getValue())
64
+ } catch (e) {
65
+ /* eslint-disable no-console */
66
+ console.error(e)
67
+ }
68
+ }
69
+ beautify() {
70
+ let editor = this._editor
71
+
72
+ let value = editor.getValue()
73
+ editor.setValue(beautify(value))
74
+ }
75
+ copy() {
76
+ copy(this._editor.getValue())
77
+ eruda.get().notify('Copied', { icon: 'success' })
78
+ }
79
+ clear() {
80
+ this._editor.setValue('')
81
+ }
82
+ destroy() {
83
+ super.destroy()
84
+ evalCss.remove(this._style)
85
+ evalCss.remove(this._CodeMirrorStyle)
86
+ evalCss.remove(this._CodeMirrorCustomStyle)
87
+ }
88
+ _bindEvent() {
89
+ this._$el
90
+ .on('click', '.eruda-run', () => this.run())
91
+ .on('click', '.eruda-beautify', () => this.beautify())
92
+ .on('click', '.eruda-clear', () => this.clear())
93
+ .on('click', '.eruda-copy', () => this.copy())
94
+ }
95
+ }
96
+
97
+ let evalJs = (code) => {
98
+ let ret
99
+
100
+ try {
101
+ ret = eval.call(window, `(${code})`)
102
+ } catch (e) {
103
+ ret = eval.call(window, code)
104
+ }
105
+
106
+ return ret
107
+ }
108
+
109
+ return new Code()
110
+ }
src/index.js ADDED
@@ -0,0 +1 @@
 
 
1
+ module.exports = require('./code').default
src/style.scss ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ $anim-duration: 0.3s;
2
+
3
+ .code {
4
+ padding-bottom: 40px;
5
+ .editor {
6
+ height: 100%;
7
+ }
8
+ .bottom-bar {
9
+ height: 40px;
10
+ background: var(--darker-background);
11
+ position: absolute;
12
+ left: 0;
13
+ bottom: 0;
14
+ width: 100%;
15
+ font-size: 0;
16
+ border-top: 1px solid var(--border);
17
+ .btn {
18
+ cursor: pointer;
19
+ text-align: center;
20
+ color: var(--foreground);
21
+ font-size: 14px;
22
+ line-height: 40px;
23
+ width: 25%;
24
+ border-right: 1px solid var(--border);
25
+ display: inline-block;
26
+ transition: background-color $anim-duration, color $anim-duration;
27
+ &:active {
28
+ background: var(--highlight);
29
+ color: var(--select-foreground);
30
+ }
31
+ &.active {
32
+ color: var(--select-foreground);
33
+ }
34
+ }
35
+ }
36
+ }
webpack.config.js ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const autoprefixer = require('autoprefixer')
2
+ const postcss = require('postcss')
3
+ const webpack = require('webpack')
4
+ const path = require('path')
5
+ const pkg = require('./package.json')
6
+ const classPrefix = require('postcss-class-prefix')
7
+ const TerserPlugin = require('terser-webpack-plugin')
8
+
9
+ const banner = pkg.name + ' v' + pkg.version + ' ' + pkg.homepage
10
+
11
+ module.exports = (env, argv) => {
12
+ const config = {
13
+ devtool: 'source-map',
14
+ entry: './src/index.js',
15
+ devServer: {
16
+ static: {
17
+ directory: path.join(__dirname, './'),
18
+ },
19
+ port: 8080,
20
+ },
21
+ output: {
22
+ path: __dirname,
23
+ filename: 'eruda-code.js',
24
+ publicPath: '/assets/',
25
+ library: ['erudaCode'],
26
+ libraryTarget: 'umd',
27
+ },
28
+ module: {
29
+ rules: [
30
+ {
31
+ test: /\.js$/,
32
+ exclude: /node_modules/,
33
+ use: {
34
+ loader: 'babel-loader',
35
+ options: {
36
+ sourceType: 'unambiguous',
37
+ presets: ['@babel/preset-env'],
38
+ plugins: ['@babel/plugin-transform-runtime'],
39
+ },
40
+ },
41
+ },
42
+ {
43
+ test: /\.scss$/,
44
+ use: [
45
+ 'css-loader',
46
+ {
47
+ loader: 'postcss-loader',
48
+ options: {
49
+ postcssOptions: {
50
+ plugins: [
51
+ postcss.plugin('postcss-namespace', function () {
52
+ // Add '.dev-tools .tools ' to every selector.
53
+ return function (root) {
54
+ root.walkRules(function (rule) {
55
+ if (!rule.selectors) return rule
56
+
57
+ rule.selectors = rule.selectors.map(function (
58
+ selector
59
+ ) {
60
+ return '.dev-tools .tools ' + selector
61
+ })
62
+ })
63
+ }
64
+ }),
65
+ classPrefix('eruda-'),
66
+ autoprefixer,
67
+ ],
68
+ },
69
+ },
70
+ },
71
+ 'sass-loader',
72
+ ],
73
+ },
74
+ {
75
+ test: /\.css$/,
76
+ use: [
77
+ 'css-loader',
78
+ {
79
+ loader: 'postcss-loader',
80
+ options: {
81
+ postcssOptions: {
82
+ plugins: [
83
+ postcss.plugin('postcss-namespace', function () {
84
+ // Add '.dev-tools .tools ' to every selector.
85
+ return function (root) {
86
+ root.walkRules(function (rule) {
87
+ if (!rule.selectors) return rule
88
+
89
+ rule.selectors = rule.selectors.map(function (
90
+ selector
91
+ ) {
92
+ return '.eruda-dev-tools .eruda-tools ' + selector
93
+ })
94
+ })
95
+ }
96
+ }),
97
+ autoprefixer,
98
+ ],
99
+ },
100
+ },
101
+ },
102
+ ],
103
+ },
104
+ ],
105
+ },
106
+ plugins: [new webpack.BannerPlugin(banner)],
107
+ }
108
+
109
+ if (argv.mode === 'production') {
110
+ config.optimization = {
111
+ minimize: true,
112
+ minimizer: [
113
+ new TerserPlugin({
114
+ terserOptions: {
115
+ format: {
116
+ comments: false,
117
+ },
118
+ },
119
+ extractComments: false,
120
+ }),
121
+ ],
122
+ }
123
+ }
124
+
125
+ return config
126
+ }