Browse Source

Create title

Kandrashin Denis 12 years ago
parent
commit
1f3d22d763
4 changed files with 18 additions and 6 deletions
  1. 10 3
      source/fb2text.cpp
  2. 1 0
      source/fb2text.hpp
  3. 5 1
      source/js/section_get.js
  4. 2 2
      source/js/section_new.js

+ 10 - 3
source/fb2text.cpp

@@ -277,25 +277,32 @@ void FbTextPage::insertDate()
 {
 }
 
-void FbTextPage::createSection()
+void FbTextPage::createDiv(const QString &className)
 {
     // $(document).children("html").children("body").children("div.body").children("div.section").get(0)
+    QString style = className;
     static const QString js1 = FB2::read(":/js/section_get.js");
     QString result = mainFrame()->evaluateJavaScript(js1).toString();
     int pos = result.indexOf("|");
     if (pos == 0) return;
     const QString location = result.left(pos);
     const QString position = result.mid(pos + 1);
+    if (style == "title" && position.left(2) != "0,") style.prepend("sub");
     FbTextElement original = element(location);
     FbTextElement duplicate = original.clone();
     original.appendOutside(duplicate);
     original.takeFromDocument();
-    static const QString js2 = FB2::read(":/js/section_new.js") + ";f(this,%1)";
-    duplicate.evaluateJavaScript(js2.arg(position));
+    static const QString js2 = FB2::read(":/js/section_new.js") + ";f(this,'%1',%2)";
+    duplicate.evaluateJavaScript(js2.arg(style).arg(position));
     QUndoCommand * command = new FbReplaceCmd(original, duplicate);
     push(command, tr("Create section"));
 }
 
+void FbTextPage::createSection()
+{
+    createDiv("section");
+}
+
 void FbTextPage::deleteSection()
 {
     FbTextElement element = current();

+ 1 - 0
source/fb2text.hpp

@@ -96,6 +96,7 @@ public slots:
 
 protected:
     virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
+    void createDiv(const QString &className);
 
 protected:
     static QString div(const QString &style, const QString &text);

+ 5 - 1
source/js/section_get.js

@@ -23,5 +23,9 @@ while(end.parentNode!==root) {
  if(end===null)return;
  end=end.parentNode;
 }
-return location(root) + "|" + $(root).children().index(start) + "," + $(root).children().index(end);
+return location(root)
++"|"+$(root).children().index(start)
++","+$(root).children().index(end)
++","+range.startOffset
++","+range.endOffset;
 })()

+ 2 - 2
source/js/section_new.js

@@ -1,11 +1,11 @@
-f=function(elem,start,end){
+f=function(elem,style,start,end){
 start=$(elem).children().get(start);
 end=$(elem).children().get(end);
 var range=document.createRange();
 range.setStartBefore(start);
 range.setEndAfter(end);
 var newNode=document.createElement("div");
-newNode.className="section";
+newNode.className=style;
 range.surroundContents(newNode);
 range.setEndBefore(start);
 var selection=window.getSelection();