瀏覽代碼

Java script: insert title

Kandrashin Denis 13 年之前
父節點
當前提交
d8ddf7d53b
共有 8 個文件被更改,包括 113 次插入69 次删除
  1. 65 64
      fb2edit.pro
  2. 1 0
      source/fb2main.cpp
  3. 6 0
      source/fb2view.cpp
  4. 1 0
      source/fb2view.hpp
  5. 34 0
      source/js/insert_title.js
  6. 1 0
      source/js/javascript.qrc
  7. 二進制
      source/ts/ru.qm
  8. 5 5
      source/ts/ru.ts

+ 65 - 64
fb2edit.pro

@@ -1,64 +1,65 @@
-HEADERS = \
-    source/fb2xml.h \
-    source/fb2xml2.h \
-    source/fb2app.hpp \
-    source/fb2code.hpp \
-    source/fb2head.hpp \
-    source/fb2main.hpp \
-    source/fb2read.hpp \
-    source/fb2temp.hpp \
-    source/fb2tree.hpp \
-    source/fb2view.hpp \
-    source/fb2utils.h \
-    source/fb2save.hpp \
-    source/fb2dlgs.hpp
-
-SOURCES = \
-    source/fb2app.cpp \
-    source/fb2code.cpp \
-    source/fb2head.cpp \
-    source/fb2main.cpp \
-    source/fb2read.cpp \
-    source/fb2save.cpp \
-    source/fb2temp.cpp \
-    source/fb2tree.cpp \
-    source/fb2view.cpp \
-    source/fb2xml.cpp \
-    source/fb2xml2.cpp \
-    source/fb2utils.cpp \
-    source/fb2dlgs.cpp
-
-RESOURCES = \
-    3rdparty/gnome/gnome.qrc \
-    source/res/fb2edit.qrc \
-    source/js/javascript.qrc
-
-TARGET = fb2edit
-
-TRANSLATIONS = source/ts/ru.ts
-
-VERSION = 0.01.1
-
-QT += xml
-QT += webkit
-QT += network
-
-OTHER_FILES += \
-    source/res/style.css \
-    source/res/blank.fb2 \
-    source/js/export.js \
-    source/js/set_cursor.js \
-    source/js/get_status.js \
-    source/js/get_location.js
-
-if (unix) {
-
-    DEFINES += FB2_USE_LIBXML2
-    INCLUDEPATH += /usr/include/libxml2
-    LIBS += -lxml2
-
-}
-
-FORMS += \
-    source/fb2note.ui \
-    source/fb2find.ui
+HEADERS = \
+    source/fb2xml.h \
+    source/fb2xml2.h \
+    source/fb2app.hpp \
+    source/fb2code.hpp \
+    source/fb2head.hpp \
+    source/fb2main.hpp \
+    source/fb2read.hpp \
+    source/fb2temp.hpp \
+    source/fb2tree.hpp \
+    source/fb2view.hpp \
+    source/fb2utils.h \
+    source/fb2save.hpp \
+    source/fb2dlgs.hpp
+
+SOURCES = \
+    source/fb2app.cpp \
+    source/fb2code.cpp \
+    source/fb2head.cpp \
+    source/fb2main.cpp \
+    source/fb2read.cpp \
+    source/fb2save.cpp \
+    source/fb2temp.cpp \
+    source/fb2tree.cpp \
+    source/fb2view.cpp \
+    source/fb2xml.cpp \
+    source/fb2xml2.cpp \
+    source/fb2utils.cpp \
+    source/fb2dlgs.cpp
+
+RESOURCES = \
+    3rdparty/gnome/gnome.qrc \
+    source/res/fb2edit.qrc \
+    source/js/javascript.qrc
+
+TARGET = fb2edit
+
+TRANSLATIONS = source/ts/ru.ts
+
+VERSION = 0.01.1
+
+QT += xml
+QT += webkit
+QT += network
+
+OTHER_FILES += \
+    source/res/style.css \
+    source/res/blank.fb2 \
+    source/js/export.js \
+    source/js/set_cursor.js \
+    source/js/get_status.js \
+    source/js/get_location.js \
+    source/js/insert_title.js
+
+if (unix) {
+
+    DEFINES += FB2_USE_LIBXML2
+    INCLUDEPATH += /usr/include/libxml2
+    LIBS += -lxml2
+
+}
+
+FORMS += \
+    source/fb2note.ui \
+    source/fb2find.ui

+ 1 - 0
source/fb2main.cpp

@@ -676,6 +676,7 @@ void Fb2MainWindow::viewText()
     connect(actionImage, SIGNAL(triggered()), textEdit, SLOT(insertImage()));
     connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
     connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
+    connect(actionTtile, SIGNAL(triggered()), textEdit, SLOT(insertTitle()));
 
     connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
     connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));

+ 6 - 0
source/fb2view.cpp

@@ -344,3 +344,9 @@ void Fb2WebView::loadFinished()
 {
     selectText(this, "var element=$('div.body').get(0);if(element===undefined)element=document.body");
 }
+
+void Fb2WebView::insertTitle()
+{
+    static const QString javascript = FB2::read(":/js/insert_title.js");
+    page()->mainFrame()->evaluateJavaScript(javascript);
+}

+ 1 - 0
source/fb2view.hpp

@@ -97,6 +97,7 @@ public slots:
     void insertImage();
     void insertNote();
     void insertLink();
+    void insertTitle();
     void zoomIn();
     void zoomOut();
     void zoomReset();

+ 34 - 0
source/js/insert_title.js

@@ -0,0 +1,34 @@
+var insert = function(parent) {
+    var child = document.createElement('DIV');
+    child.setAttribute("class", 'title');
+    child.innerText = "title";
+    parent.insertAdjacentElement("afterBegin",child);
+    return child;
+}
+
+var section = function(parent) {
+    if (parent.nodeName !== 'DIV') return 0;
+    var attr = parent.getAttribute('CLASS').toLowerCase();
+    if (attr === 'section') return 1;
+    return 0;
+}
+
+var exists = function(parent) {
+    var child = parent.firstChild;
+    if (child === undefined) return 0;
+    if (child.nodeName !== 'DIV') return 0;
+    if (child.getAttribute('CLASS').toLowerCase() !== 'title') return 0;
+    return 1;
+}
+
+var parent = document.getSelection().baseNode;
+
+while (parent.nodeName !== 'BODY') {
+  if (parent.nodeName === 'DIV') {
+      if (!section(parent)) break;
+      if (exists(parent)) break;
+      insert(parent);
+      break;
+  }
+  parent = parent.parentNode;
+}

+ 1 - 0
source/js/javascript.qrc

@@ -5,5 +5,6 @@
         <file>get_status.js</file>
         <file>get_location.js</file>
         <file>set_cursor.js</file>
+        <file>insert_title.js</file>
     </qresource>
 </RCC>

二進制
source/ts/ru.qm


+ 5 - 5
source/ts/ru.ts

@@ -406,23 +406,23 @@
     </message>
     <message>
         <source>&amp;Body</source>
-        <translation>&amp;Тело &lt;body&gt;</translation>
+        <translation>Тело &lt;body&gt;</translation>
     </message>
     <message>
         <source>Zoom original</source>
-        <translation type="unfinished"></translation>
+        <translation></translation>
     </message>
     <message>
         <source>&amp;Section</source>
-        <translation>&amp;Секция &lt;section&gt;</translation>
+        <translation>Секция &lt;section&gt;</translation>
     </message>
     <message>
         <source>&amp;Title</source>
-        <translation>&amp;Заголовок &lt;title&gt;</translation>
+        <translation>Заголовок &lt;title&gt;</translation>
     </message>
     <message>
         <source>&amp;Author</source>
-        <translation>&amp;Автор &lt;author&gt;</translation>
+        <translation>Автор &lt;author&gt;</translation>
     </message>
     <message>
         <source>&amp;Annotation</source>