def myKeyDown(self, event): if event.keyCode == 37: # left arrow sel = self.get_col() if sel < 1: event.preventDefault() event.stopPropagation() elif event.keyCode == 36: # line start window.getSelection().collapseToStart() event.preventDefault() elif event.keyCode == 38: # up if self.current > 0: pass # remove current line # set_element_value(element_value()[:len(PROMPT)]) # current -= 1 # append_to_element(history[current]) event.preventDefault() elif event.keyCode == 40: # down if self.current < len(self.history) - 1: pass # pos = element.selectionStart # col = get_col(element) # # remove current line # set_element_value(element_value()[:pos - col + 4]) # current += 1 # append_to_element(history[current]) event.preventDefault() elif event.keyCode == 8: # backspace sel = self.get_col() if sel < 1: event.preventDefault() event.stopPropagation()
def cursorToEnd(self, event=None): _range = doc.createRange() _range.selectNodeContents(self.element) _range.collapse(False) selection = window.getSelection() selection.removeAllRanges() selection.addRange(_range)
def focus(ev): """Cell gets focus""" # save initial value in case user hits the Escape key selected.initial = ev.target.text # set position cursor at text end _range = document.createRange() sel = window.getSelection() if len(selected.childNodes): _range.setStart(selected.firstChild, len(ev.target.text)) _range.collapse(True) sel.removeAllRanges() sel.addRange(_range) document["current"].text = ev.target.text
def focus(self, ev): """Cell gets focus""" # save initial value in case user hits the Escape key self.initial = self.text # set position cursor at text end _range = document.createRange() sel = window.getSelection() if len(self.childNodes): _range.setStart(self.firstChild, len(self.text)) _range.collapse(True) sel.removeAllRanges() sel.addRange(_range) document["current"].text = self.text
def keydown(self, evt): if evt.keyCode == 9: # tab evt.preventDefault() sel = window.getSelection() start = self.editor.selectionStart end = self.editor.selectionEnd self.editor.value = (self.editor.value[:start] + " " + editor.value[end:]) elif evt.keyCode == 13: start = self.editor.selectionStart line = self.editor.value[:start].split("\n")[-1] self.indent = len(line) - len(line.lstrip()) if line.rstrip().endswith(":"): self.indent += 4
def insertTextAtCursor(self, text): try: sel = window.getSelection() _range = sel.getRangeAt(0) _range.deleteContents() _range.insertNode(doc.createTextNode(text)) except: pass try: doc.selection.createRange().text = text except: pass self.cursorToEnd()
def get_col(self): # returns the column num of cursor # it counts after hte <span>, we do -1 # because the prompt has a space after it _range = window.getSelection().getRangeAt(0) return _range.startOffset - 1