Forráskód Böngészése

Remove section: undo, redo action

Kandrashin Denis 12 éve
szülő
commit
a2a545fc87
4 módosított fájl, 56 hozzáadás és 20 törlés
  1. 27 6
      source/fb2html.cpp
  2. 4 2
      source/fb2html.h
  3. 24 11
      source/fb2text.cpp
  4. 1 1
      source/js/section_new.js

+ 27 - 6
source/fb2html.cpp

@@ -232,6 +232,27 @@ bool FbTextElement::hasTitle() const
     return FbTextElement(firstChild()).isTitle();
 }
 
+int FbTextElement::index() const
+{
+    int result = -1;
+    FbTextElement prior = *this;
+    while (!prior.isNull()) {
+        prior = prior.previousSibling();
+        result++;
+    }
+    return result;
+}
+
+FbTextElement FbTextElement::child(int index) const
+{
+    FbTextElement result = firstChild();
+    while (index > 0) {
+        result = result.nextSibling();
+        index--;
+    }
+    return index ? FbTextElement() : result;
+}
+
 //---------------------------------------------------------------------------
 //  FbInsertCmd
 //---------------------------------------------------------------------------
@@ -267,10 +288,10 @@ void FbInsertCmd::undo()
 //  FbReplaceCmd
 //---------------------------------------------------------------------------
 
-FbReplaceCmd::FbReplaceCmd(const FbTextElement &original, const FbTextElement &element)
+FbReplaceCmd::FbReplaceCmd(const FbTextElement &original, const FbTextElement &duplicate)
     : QUndoCommand()
     , m_original(original)
-    , m_element(element)
+    , m_duplicate(duplicate)
     , m_update(false)
 {
 }
@@ -278,9 +299,9 @@ FbReplaceCmd::FbReplaceCmd(const FbTextElement &original, const FbTextElement &e
 void FbReplaceCmd::redo()
 {
     if (m_update) {
-        m_original.prependOutside(m_element);
+        m_original.prependOutside(m_duplicate);
         m_original.takeFromDocument();
-        m_element.select();
+        m_duplicate.select();
     } else {
         m_update = true;
     }
@@ -288,8 +309,8 @@ void FbReplaceCmd::redo()
 
 void FbReplaceCmd::undo()
 {
-    m_element.prependOutside(m_original);
-    m_element.takeFromDocument();
+    m_duplicate.prependOutside(m_original);
+    m_duplicate.takeFromDocument();
     m_original.select();
 }
 

+ 4 - 2
source/fb2html.h

@@ -59,10 +59,12 @@ public:
     FbTextElement(const QWebElement &x) : QWebElement(x) {}
     FbTextElement &operator=(const QWebElement &x) { QWebElement::operator=(x); return *this; }
     FbTextElement insertInside(const QString &style, const QString &html);
+    FbTextElement child(int index) const;
     void getChildren(FbElementList &list);
     bool hasSubtype(const QString &style) const;
     bool hasScheme() const;
     QString location();
+    int index() const;
 
 public:
     bool hasChild(const QString &style) const;
@@ -105,12 +107,12 @@ private:
 class FbReplaceCmd : public QUndoCommand
 {
 public:
-    explicit FbReplaceCmd(const FbTextElement &original, const FbTextElement &element);
+    explicit FbReplaceCmd(const FbTextElement &original, const FbTextElement &duplicate);
     virtual void undo();
     virtual void redo();
 private:
     FbTextElement m_original;
-    FbTextElement m_element;
+    FbTextElement m_duplicate;
     bool m_update;
 };
 

+ 24 - 11
source/fb2text.cpp

@@ -287,12 +287,12 @@ void FbTextPage::createSection()
     const QString location = result.left(pos);
     const QString position = result.mid(pos + 1);
     FbTextElement original = element(location);
-    FbTextElement section = original.clone();
-    original.appendOutside(section);
+    FbTextElement duplicate = original.clone();
+    original.appendOutside(duplicate);
     original.takeFromDocument();
     static const QString js2 = FB2::read(":/js/section_new.js") + ";f(this,%1)";
-    section.evaluateJavaScript(js2.arg(position));
-    QUndoCommand * command = new FbReplaceCmd(original, section);
+    duplicate.evaluateJavaScript(js2.arg(position));
+    QUndoCommand * command = new FbReplaceCmd(original, duplicate);
     push(command, tr("Create section"));
 }
 
@@ -301,15 +301,25 @@ void FbTextPage::deleteSection()
     FbTextElement element = current();
     while (!element.isNull()) {
         if (element.isSection()) {
-            FbTextElement parent = element.parent();
-            FbTextElement subling = element.previousSibling();
+            if (element.parent().isBody()) return;
+            FbTextElement original = element.parent();
+            FbTextElement duplicate = original.clone();
+            int index = element.index();
+            original.appendOutside(duplicate);
+            original.takeFromDocument();
+            element = duplicate.child(index);
+            if (index) {
+                FbTextElement title = element.firstChild();
+                if (title.isTitle()) {
+                    title.removeClass("title");
+                    title.addClass("subtitle");
+                }
+            }
             QString xml = element.toInnerXml();
             element.setOuterXml(xml);
-            if (subling.isNull()) {
-                parent.select();
-            } else {
-                subling.nextSibling().select();
-            }
+            QUndoCommand * command = new FbReplaceCmd(original, duplicate);
+            push(command, tr("Remove section"));
+            element.select();
             break;
         }
         element = element.parent();
@@ -367,6 +377,9 @@ void FbTextPage::fixContents()
     foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
         span.removeAttribute("style");
     }
+    foreach (QWebElement span, doc().findAll("[style]")) {
+        span.removeAttribute("style");
+    }
 }
 
 //---------------------------------------------------------------------------

+ 1 - 1
source/js/section_new.js

@@ -1,4 +1,4 @@
-f=function(elem,start, end){
+f=function(elem,start,end){
 start=$(elem).children().get(start);
 end=$(elem).children().get(end);
 var range=document.createRange();