Skip to content

Commit b795789

Browse files
committed
fix codemirror refresh issue and disable auto load mode
1 parent 0da4078 commit b795789

File tree

5 files changed

+96
-8
lines changed

5 files changed

+96
-8
lines changed

pywebio/html/codemirror/addons.js

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2+
// Distributed under an MIT license: https://codemirror.net/LICENSE
3+
4+
(function(mod) {
5+
if (typeof exports == "object" && typeof module == "object") // CommonJS
6+
mod(require("../../lib/codemirror"))
7+
else if (typeof define == "function" && define.amd) // AMD
8+
define(["../../lib/codemirror"], mod)
9+
else // Plain browser env
10+
mod(CodeMirror)
11+
})(function(CodeMirror) {
12+
"use strict"
13+
14+
CodeMirror.defineOption("autoRefresh", false, function(cm, val) {
15+
if (cm.state.autoRefresh) {
16+
stopListening(cm, cm.state.autoRefresh)
17+
cm.state.autoRefresh = null
18+
}
19+
if (val && cm.display.wrapper.offsetHeight == 0)
20+
startListening(cm, cm.state.autoRefresh = {delay: val.delay || 250})
21+
})
22+
23+
function startListening(cm, state) {
24+
function check() {
25+
if (cm.display.wrapper.offsetHeight) {
26+
stopListening(cm, state)
27+
if (cm.display.lastWrapHeight != cm.display.wrapper.clientHeight)
28+
cm.refresh()
29+
} else {
30+
state.timeout = setTimeout(check, state.delay)
31+
}
32+
}
33+
state.timeout = setTimeout(check, state.delay)
34+
state.hurry = function() {
35+
clearTimeout(state.timeout)
36+
state.timeout = setTimeout(check, 50)
37+
}
38+
CodeMirror.on(window, "mouseup", state.hurry)
39+
CodeMirror.on(window, "keyup", state.hurry)
40+
}
41+
42+
function stopListening(_cm, state) {
43+
clearTimeout(state.timeout)
44+
CodeMirror.off(window, "mouseup", state.hurry)
45+
CodeMirror.off(window, "keyup", state.hurry)
46+
}
47+
});

pywebio/platform/tpl/index.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@
4646

4747
<script src="{{ base_url }}js/mustache.min.js"></script> <!--template system-->
4848
<script src="{{ base_url }}js/codemirror.min.js"></script> <!--code textarea editor-->
49-
<script src="{{ base_url }}codemirror/matchbrackets.js"></script> <!--codemirror plugin-->
50-
<script src="{{ base_url }}codemirror/python.js"></script> <!--codemirror python language support-->
51-
<script src="{{ base_url }}codemirror/loadmode.js"></script> <!--codemirror plugin: auto load mode-->
52-
<script src="{{ base_url }}codemirror/active-line.js"></script> <!--codemirror plugin: auto load mode-->
49+
<script src="{{ base_url }}codemirror/addons.js"></script> <!--codemirror addons: matchbrackets, python mode, active line, auto refresh-->
5350
<script src="{{ base_url }}js/prism.min.js"></script> <!-- markdown code highlight -->
5451
<script src="{{ base_url }}js/FileSaver.min.js"></script> <!-- saving files on the client-side -->
5552
<script src="{{ base_url }}js/jquery.min.js"></script>

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"html/codemirror/active-line.js",
4545
"html/codemirror/matchbrackets.js",
4646
"html/codemirror/loadmode.js",
47+
"html/codemirror/autorefresh.js",
48+
"html/codemirror/addons.js",
4749
"html/codemirror/python.js",
4850
"html/css/bootstrap.min.css",
4951
"html/css/markdown.min.css",

webiojs/src/models/input/textarea.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class Textarea extends InputItem {
2525
'styleActiveLine': true, // 当前行背景高亮
2626
'matchBrackets': true, //括号匹配
2727
'lineWrapping': true, //自动换行
28+
'autoRefresh': true // https://codemirror.net/doc/manual.html#addon_autorefresh
2829
};
2930

3031
constructor(spec: any, task_id: string, on_input_event: (event_name: string, input_item: InputItem) => void) {
@@ -90,10 +91,12 @@ export class Textarea extends InputItem {
9091
after_show(first_show: boolean): any {
9192
if (first_show && this.spec.code) {
9293
this.code_mirror = CodeMirror.fromTextArea(this.element.find('textarea')[0], this.code_mirror_config);
93-
try {
94-
CodeMirror.autoLoadMode(this.code_mirror, this.code_mirror_config.mode);
95-
} catch (e) {
96-
console.error('CodeMirror load mode `%s` error: %s', this.code_mirror_config.mode, e);
94+
if (CodeMirror.autoLoadMode) {
95+
try {
96+
CodeMirror.autoLoadMode(this.code_mirror, this.code_mirror_config.mode);
97+
} catch (e) {
98+
console.error('CodeMirror load mode `%s` error: %s', this.code_mirror_config.mode, e);
99+
}
97100
}
98101
if (this.spec.onchange)
99102
this.code_mirror.on('change', (instance: object, changeObj: object) => {

0 commit comments

Comments
 (0)