From 6d95b25dbd69fff9bf25e43fbebf813cb3046f0b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 31 Oct 2017 12:17:07 +0530 Subject: [PATCH] Edit Book: When adding a new HTML file, add it after the file being currently edited instead of at the end. Fixes #1728601 [[enhancement] placement in file browser of the editor when adding or importing file](https://bugs.launchpad.net/calibre/+bug/1728601) --- src/calibre/ebooks/oeb/polish/container.py | 8 ++++++++ src/calibre/gui2/tweak_book/boss.py | 9 ++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index 5161f7166f..ceecb12153 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -744,6 +744,14 @@ class Container(ContainerBase): # {{{ for item, name in non_linear: yield item, name, False + def index_in_spine(self, name): + manifest_id_map = self.manifest_id_map + for i, item in enumerate(self.opf_xpath('//opf:spine/opf:itemref[@idref]')): + idref = item.get('idref') + q = manifest_id_map.get(idref, None) + if q == name: + return i + @property def spine_names(self): ''' An iterator yielding name and is_linear for every item in the diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 28d30c666c..e10a96f3b9 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -433,7 +433,7 @@ class Boss(QObject): completion_worker().clear_caches('names') def add_file(self): - if not self.ensure_book(_('You must first open a book to tweak, before trying to create new files in it.')): + if not self.ensure_book(_('You must first open a book to edit, before trying to create new files in it.')): return self.commit_dirty_opf() d = NewFileDialog(self.gui) @@ -448,8 +448,11 @@ class Boss(QObject): self.add_savepoint(_('Before: Add file %s') % self.gui.elided_text(file_name)) c = current_container() adata = data.replace(b'%CURSOR%', b'') if using_template else data + spine_index = c.index_in_spine(self.currently_editing or '') + if spine_index is not None: + spine_index += 1 try: - added_name = c.add_file(file_name, adata) + added_name = c.add_file(file_name, adata, spine_index=spine_index) except: self.rewind_savepoint() raise @@ -469,7 +472,7 @@ class Boss(QObject): return added_name def add_files(self): - if not self.ensure_book(_('You must first open a book to tweak, before trying to create new files in it.')): + if not self.ensure_book(_('You must first open a book to edit, before trying to create new files in it.')): return files = choose_files(self.gui, 'tweak-book-bulk-import-files', _('Choose files'))