Skip to content

Commit

Permalink
Fix readonly (#158)
Browse files Browse the repository at this point in the history
* tweak test runner in web-demo.html

* fix readonly mode

* remove stray console.log

Co-authored-by: Sergei Chestakov <[email protected]>

---------

Co-authored-by: Sergei Chestakov <[email protected]>
  • Loading branch information
nightwing and sergeichestakov authored Jan 11, 2024
1 parent 81fafd4 commit ff439cb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
8 changes: 6 additions & 2 deletions dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,18 @@ var options = {
split: addOption("split", "", function() {

}),
readOnly: addOption("readOnly")
};



Vim.defineOption('wrap', false, 'boolean', null, function(val, cm) {
if (val == undefined) return options.wrap;
var checkbox = document.getElementById("wrap");
checkbox.checked = val;
checkbox.onclick();
if (checkbox) {
checkbox.checked = val;
checkbox.onclick();
}
});

var focusEditorButton = document.createElement("button");
Expand Down Expand Up @@ -188,6 +191,7 @@ function updateView() {
selectTab(options.html ? "html": "js")

var extensions = [
EditorState.readOnly.of(options.readOnly),
enableVim && vim({status: options.status}),
options.wrap && EditorView.lineWrapping,
drawSelection({cursorBlinkRate: window.blinkRate})
Expand Down
91 changes: 87 additions & 4 deletions dev/web-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<style>
#editor {
height: min(540px, 50vh); width: min(960px, 90vw);
position: absolute; resize: both; overflow: hidden
resize: both; overflow: hidden
}
#editor>.cm-editor {width: 100%; height: 100%}
#editor.split>.cm-editor {height: 50%}
Expand Down Expand Up @@ -469,7 +469,7 @@
if (!/^\w+:/.test(path)) path = host + path;
var onLoad = function(e, val) {
if (e) return processLoadQueue({id: id, path: path});
if (path.slice(-3) == ".ts") {
if (path.slice(-3) == ".ts" || /^import \{/m.test(val)) {
val = ts.transpileModule(val, {
compilerOptions: {
target: "ES2022",
Expand Down Expand Up @@ -520,7 +520,12 @@
}
} else {
var url = require.toUrl(module, ".js");
if (define.fetchedUrls[url] & 1) return false;
if (define.fetchedUrls[url] & 1) {
setTimeout(function() {
processLoadQueue(null, module);
});
return false;
}
define.fetchedUrls[url] |= 1;
loadScript(url, module, processLoadQueue);
}
Expand Down Expand Up @@ -603,6 +608,10 @@
<body>
<div id="toolbar"></div>
<div id="editor"></div>
<br>
<br>
<a href="#test.html" onclick="runTests()">tests</a><br>
<a href="./cm5/index.html">cm5 demo</a>
<script>
// console.log(ts)
require.config({
Expand Down Expand Up @@ -630,11 +639,85 @@
"src/block-cursor": require.config.baseUrl.replace("dev", "src/block-cursor.ts"),
"src/cm_adapter": require.config.baseUrl.replace("dev", "src/cm_adapter.ts"),
"src/vim": require.config.baseUrl.replace("dev", "src/vim.js"),

}
});

require(Object.keys(require.config.paths), function() {})
</script>
<script>
var it, describe
var old
function runTests() {
if (!old) {
old = {};
Object.keys(define.modules).forEach(k=> old[k] = 1)
} else {
Object.keys(define.modules)
.concat(Object.keys(define.errors))
.forEach(k=> {
if (!old[k])
require.undef(k)
})
}

require.config({
paths: {
"test/webtest-vim": require.config.baseUrl.replace("dev", "test/webtest-vim.js"),
"test/vim_test.js": require.config.baseUrl.replace("dev", "test/vim_test.js"),
}
});
define("test/..", function(require, exports, module) {
module.exports = require("src/index")
});
var suites = Object.create(null)
describe = function(name, fn) {
var suite = {
name,
construct: fn,
tests: Object.create(null),
skipped: Object.create(null),
it: function (name, fn) {
this.tests[name] = fn;
},
skip: function (name, fn) {
this.skipped[name] = fn;
},
};
suites[name] = suite;
it = suite.it = suite.it.bind(suite);
it.skip = suite.skip.bind(suite);
suite.construct();
}

require(["test/webtest-vim"], async function() {
var total = 0;
var skipped = 0;
var failed = 0;
var passed = 0;
for (var suite of Object.values(suites)) {
var skippedFromSuite = Object.keys(suite.skipped).length;
skipped += skippedFromSuite
total += Object.keys(suite.tests).length
+ skippedFromSuite;
for (var i in suite.tests) {
console.log(i)
try {
await suite.tests[i]()
passed++
} catch(e) {
failed++
console.error(e)
}
}
}
console.log(`
failed: ${failed}
passed: ${passed},
skipped: ${skipped},
from: ${total}
`)
});
}
</script>
</body>
</html>
4 changes: 3 additions & 1 deletion src/cm_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ interface Operation {
// workaround for missing api for merging transactions
function dispatchChange(cm: CodeMirror, transaction: any) {
var view = cm.cm6;
if (view.state.readOnly)
return;
var type = "input.type.compose";
if (cm.curOp) {
if (!cm.curOp.lastChange) type = "input.type.compose.start";
Expand Down Expand Up @@ -767,7 +769,7 @@ export class CodeMirror {
switch (name) {
case "firstLineNumber": return 1;
case "tabSize": return this.cm6.state.tabSize || 4;
case "readonly": return this.cm6.state.readOnly;
case "readOnly": return this.cm6.state.readOnly;
case "indentWithTabs": return this.cm6.state.facet(indentUnit) == "\t"; // TODO
case "indentUnit": return this.cm6.state.facet(indentUnit).length || 2;
case "textwidth": return this.state.textwidth;
Expand Down
11 changes: 9 additions & 2 deletions test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ testVim('on_mode_change', async function(cm, vim, helpers) {
test('v', 'visual');
test(':', ''); // Event for Command-line mode not implemented.
test('y\n', 'normal');
test(":startinsert", "insert");
test(":startinsert\n", "insert");
});

// Swapcase commands edit in place and do not modify registers.
Expand Down Expand Up @@ -5753,8 +5753,15 @@ var typeKey = function() {

var text = letter;
var isTextInput = true;
if (ctrl || alt || meta || controlKeys[letter]) {
if (ctrl || alt || meta) {
isTextInput = false;
} else if (controlKeys[letter]) {
if (keyCode == controlKeys.Return) {
text = "\n";
isTextInput = true;
} else {
isTextInput = false;
}
} else if (shift) {
text = text.toUpperCase();
}
Expand Down

0 comments on commit ff439cb

Please sign in to comment.