-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcharacterEquivalencies.js
120 lines (100 loc) · 3.53 KB
/
characterEquivalencies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
*** Provides Support for Character Equivalencies ***
*This file contains the setupCharacterEquivalencies() function that is run in
startup() when the editor first loads.
*The function creates new command keys to support automatic conversion between
!= and ≠ for example. See the 'replacements' obj. for more info.
*/
function setupCharacterEquivalencies()
{
var cursorMoved = false; //Global variable to be set when the cursor moves
//after a character replacement occurs
//Possible replacements in the editor
var replacements =
{
"!=":"≠",
">=":"≥",
"<=":"≤",
"->":"→",
"[[":"⟦",
"]]":"⟧"
};
//Possible replacements for undoing the original replace
var undoReplace =
{
"≠":"!=",
"≥":">=",
"≤":"<=",
"→":"->",
"⟦":"[[",
"⟧":"]]"
};
//Symbols to add as commands
var endSymbols = ['=','>','[',']'];
//****** Add All Character Equivalencies ********//
for(i =0; i<4; ++i)
{
var a = endSymbols[i];
addCharEq(a);
}
function addCharEq(a)
{
editor.commands.addCommand({
name: 'myCommand'+a,
bindKey: {win: a, mac: a},
exec: function(editor) {
//Insert '=' to support standard functionality
editor.insert(a);
//Set the cursor moved to false to allow backspace replacement
cursorMoved = false;
//Calculate the cursor position
var cursor = editor.getCursorPosition();
Range = require("ace/range").Range;
//Check if replacement is possible
if(cursor.column >= 2){
//Get the range and the text
var cursorLine = new Range(cursor.row, cursor.column-2, cursor.row, cursor.column);
var text = editor.session.getTextRange(cursorLine);
if(text in replacements)
{
//Insert the matching symbol
editor.session.replace(cursorLine, replacements[text]);
//Store text for undo
lastReplacement = text;
}
}
},
readOnly: false // false if this command should not apply in readOnly mode
});
}
//****** BACKSPACE Command Key ********//
editor.commands.addCommand({
name: 'BACK',
bindKey: {win: 'backspace', mac: 'backspace'},
exec: function(editor) {
var cursor = editor.getCursorPosition();
Range = require("ace/range").Range;
//Check if there is text here, then look for !=
if(cursor.column >= 1){
var cursorLine = new Range(cursor.row, cursor.column-1, cursor.row, cursor.column);
var text = editor.session.getTextRange(cursorLine);
if(text in undoReplace && !cursorMoved)
{
//Replace text - return true to signal overriden functionality
editor.session.replace(cursorLine,undoReplace[text]);
return true;
}
}
//Return false to signal regular backspace functionality
return false;
},
readOnly: false // false if this command should not apply in readOnly mode
});
//****** Cursor Move Check ********//
//Editor check to see if the cursor has moved
//used to provide dynamic replacement functionality
editor.getSession().selection.on('changeSelection', function(e)
{
cursorMoved = true;
});
}