Skip to content

Adds Mouse Event Handlers #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions css/custom.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#code_editor {
background-color: rgb(39, 39, 39);
width: 800px;
height: 400px;
overflow: auto;
overflow-y: auto;
/* overflow-x: hidden; */
}
.caret {
transition: 0.06s;
}
.linenum {
color: whitesmoke;
width: 40px;
}
.code {
color: white;
Expand All @@ -19,10 +22,10 @@
display: inline-block;
}
.value {
color: rgb(253, 2, 136);
color: rgb(253, 136, 2);
display: inline-block;
}
.comment {
color: green;
color: rgb(7, 212, 7);
display: inline-block;
}
14 changes: 10 additions & 4 deletions css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@ body {
padding: 0;
width: 100%;
height: 100%;
overflow: auto;
overflow: hidden;
}

.line {
position: relative;
top: 0px;
left: 0px;
width: calc(100% - 10px);
height: 2vh;
height: auto;

font-size: 2vh;
padding: 2px 5px;
z-index: 1;
}
.line .linenum {
position: relative;
display: inline-block;
min-height: 2vh;
text-align: center;
vertical-align: middle;
z-index: 1;
}
.line .code {
position: relative;
position: absolute;
top: 0px;
display: inline-block;
margin: 1px 5px;
min-height: 2vh;
z-index: 0;
height: auto;
z-index: 1;

word-wrap: break-all;
white-space:normal;
}

.character {
Expand Down
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
</head>

<body>
<div id="code_editor"></div>
<div id="code_editor" tabindex="0"></div>

<!-- Dependencies -->
<script src="js/jquery.min.js"></script>
<script src="js/colorize.js"></script>
<script src="js/caret.js"></script>
Expand Down
63 changes: 52 additions & 11 deletions js/caret.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,57 @@ Caret.prototype = {

let el = this.getCharacterElementBefore();
let caretElement = $(`#caret_${this.page.getId()}_${this.id}`);
let lineEl, linenumEl, linecodeEl;

let top, left;
if(this.row == 1) top = 0;
else top = $(`#${this.page.getId()} .line:nth-child(${this.row})`).position().top;
if(this.row == 1) top = 1.5;
else top = $(`#${this.page.getId()} .line:nth-child(${this.row})`).position().top + 1.5;

linenumEl = $(`#${this.page.getId()} .line:nth-child(${this.row}) .linenum`);
if(this.col == 1 || el == undefined) left = $(`#${this.page.getId()} .line:nth-child(${this.row}) .linenum`).width() + 10;
else left = $(el).position().left + $(el).width() + 16;
if(this.col !==1 && el !== undefined)
console.log(top, left)
else left = $(el).position().left + $(el).width() + $(linenumEl).width() + 10;
// if(this.col !==1 && el !== undefined)
// console.log(top, left)

$(caretElement).css('top', `${top}px`);
$(caretElement).css('left', `${left}px`);

// toggle current line background color
let lineEl = $(".curLine");
lineEl = $(".curLine");
$(lineEl).removeClass("curLine");
lineEl = $(`.line:nth-child(${this.row})`);
$(lineEl).addClass("curLine");

// TODO Enable word-wrap
// detect text overflow
// lineEl = $(`#${this.page.getId()} .line:nth-child(${this.row})`)
// let linenumEl = $(`#${this.page.getId()} .line:nth-child(${this.row}) .linenum`);
// let linecodeEl = $(`#${this.page.getId()} .line:nth-child(${this.row}) .code`);
lineEl = $(`#${this.page.getId()} .line:nth-child(${this.row})`)
linenumEl = $(`#${this.page.getId()} .line:nth-child(${this.row}) .linenum`);
linecodeEl = $(`#${this.page.getId()} .line:nth-child(${this.row}) .code`);
let charEl = $(`#${this.page.getId()} .line:nth-child(${this.row}) .code .character:nth-child(1)`);
if(charEl !== undefined) {
let linecodeWidth = $(linecodeEl).width();
let line = this.page.getLineRef(this.row);
let charCount = line.getCode().length;
let charWidth = $(charEl).width();
let isOverflow = charCount*charWidth > linecodeWidth;

if(isOverflow) {
// console.log('OVERFLOW');
// let linecodeHeight = config.linecodeHeight;
// let increase
// $(linecodeEl).height(linecodeHeight*2);
} else {
// console.log('NO OVERFLOW');
}
}

// if($(lineEl).width() < $(linenumEl).width() + $(linecodeEl).width()) {
// console.log('OVERFLOW')

// console.log('OVERFLOW');
// // let lineHeight = $(lineEl).height();
// // $(lineEl).height(lineHeight*2);
// // let linecodeHeight = $(linecodeEl).height();
// // $(linecodeEl).height(linecodeHeight*2);
// // console.log(linecodeHeight);

// } else {
// console.log('NO OVERFLOW');
Expand Down Expand Up @@ -224,5 +247,23 @@ Caret.prototype = {
newCol = this.col + 1;
}
this.setPos(newRow, newCol);
},

moveToCharacterElement: function(charEl) {
let charPos = this.page.getCharacterPosition(charEl);
this.setPos(charPos.line, charPos.index);
},

moveToEndOfLine: function(lineEl) {
let _this = this;

let line_num = lineEl.index() + 1;
let codeEl = $(`#${_this.page.id} .line:nth-child(${line_num}) > .code`);
let codePos = codeEl.offset();
let codeEnd = codePos.left + codeEl.width();
if(event.pageX > codeEnd) {
let line_ref = _this.page.getLineRef(line_num);
_this.setPos(line_num, line_ref.getCharCount()+1);
}
}
}
5 changes: 2 additions & 3 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

$(document).ready(function() {
console.log('READY');

var page0 = initNewPage('code_editor', window.innerWidth, window.innerHeight);
var page0 = new Page('code_editor', window.innerWidth, window.innerHeight);

window.onresize = function() {
page0.setWidth(window.innerWidth);
Expand Down
2 changes: 1 addition & 1 deletion js/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const keyToChar = function(keyCode) {
// --------------------------------------------------------------------------
// KEYDOWN
const handleKeyDown = function(page, keyCode) {
var tmpkey = String.fromCharCode(keyCode);
// var tmpkey = String.fromCharCode(keyCode);
// console.log(tmpkey + ' ' + keyCode);

// Ignore Keys --------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions js/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Line.prototype = {
return linenumHTML + textHTML;
},

getCharCount: function() {
return this.getCode().length;
},

setLineNum: function(num) {
this.lineNum = num;
let el = $(`#${this.page.getId()} .line:nth-child(${this.lineNum}) .linenum`);
Expand Down
Empty file added js/mouse.js
Empty file.
Loading