Codebase list sugar-read-activity / 81a7975
Implement text highlight in epub backend The UI part is working, but the changes are not permanent, we need check what html files are modified and zip again in the epub file. Gonzalo Odiard 9 years ago
1 changed file(s) with 66 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
6464 return False
6565
6666 def can_highlight(self):
67 return False
67 return True
68
69 def show_highlights(self, page):
70 # we save the highlights in the page as html
71 pass
72
73 def toggle_highlight(self, highlight):
74 self._view.set_editable(True)
75
76 if highlight:
77 self._view.execute_script(
78 'document.execCommand("backColor", false, "yellow");')
79 else:
80 # need remove the highlight nodes
81 js = """
82 var selObj = window.getSelection();
83 var range = selObj.getRangeAt(0);
84 var node = range.startContainer;
85 while (node.parentNode != null) {
86 if (node.localName == "span") {
87 if (node.hasAttributes()) {
88 var attrs = node.attributes;
89 for(var i = attrs.length - 1; i >= 0; i--) {
90 if (attrs[i].name == "style" &&
91 attrs[i].value == "background-color: yellow;") {
92 node.removeAttribute("style");
93 break;
94 };
95 };
96 };
97 };
98 node = node.parentNode;
99 };"""
100 self._view.execute_script(js)
101
102 self._view.set_editable(False)
103
104 def in_highlight(self):
105 # Verify if the selection already exist or the cursor
106 # is in a highlighted area
107 page_title = self._view.get_title()
108 js = """
109 var selObj = window.getSelection();
110 var range = selObj.getRangeAt(0);
111 var node = range.startContainer;
112 var onHighlight = false;
113 while (node.parentNode != null) {
114 if (node.localName == "span") {
115 if (node.hasAttributes()) {
116 var attrs = node.attributes;
117 for(var i = attrs.length - 1; i >= 0; i--) {
118 if (attrs[i].name == "style" &&
119 attrs[i].value == "background-color: yellow;") {
120 onHighlight = true;
121 };
122 };
123 };
124 };
125 node = node.parentNode;
126 };
127 document.title=onHighlight;"""
128 self._view.execute_script(js)
129 on_highlight = self._view.get_title() == 'true'
130 self._view.execute_script('document.title = "%s";' % page_title)
131 # the second parameter is only used in the text backend
132 return on_highlight, None
68133
69134 def can_do_text_to_speech(self):
70135 return False