Comments editor: Fix data file links not working

This commit is contained in:
Kovid Goyal 2023-11-19 10:17:35 +05:30
parent 1160486703
commit 9c84994d32
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 3 deletions

View File

@ -1333,8 +1333,14 @@ class BookDetails(DetailsLayout): # {{{
) )
def browse(url): def browse(url):
if url.startswith(DATA_DIR_NAME + '/') and self.last_data.get('book_id'):
base = get_gui().current_db.abspath(self.last_data['book_id'], index_is_id=True)
path = os.path.join(base, url)
qurl = QUrl.fromLocalFile(path)
else:
qurl = QUrl(url, QUrl.ParsingMode.TolerantMode)
try: try:
safe_open_url(QUrl(url, QUrl.ParsingMode.TolerantMode)) safe_open_url(qurl)
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
@ -1389,7 +1395,7 @@ class BookDetails(DetailsLayout): # {{{
def show_data(self, data): def show_data(self, data):
try: try:
self.last_data = {'title':data.title, 'authors':data.authors} self.last_data = {'title':data.title, 'authors':data.authors, 'book_id': getattr(data, 'id', None)}
except Exception: except Exception:
self.last_data = {} self.last_data = {}
self.book_info.show_data(data) self.book_info.show_data(data)

View File

@ -1003,6 +1003,12 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
if a <= q <= b: if a <= q <= b:
return cf return cf
def open_link(self, link: str) -> None:
qurl = QUrl(link, QUrl.ParsingMode.TolerantMode)
if qurl.isRelative() and self.base_url:
qurl = QUrl.fromLocalFile(os.path.join(os.path.dirname(self.base_url.toLocalFile()), qurl.path()))
safe_open_url(qurl)
def contextMenuEvent(self, ev): def contextMenuEvent(self, ev):
menu = QMenu(self) menu = QMenu(self)
img_name = self.document().documentLayout().imageAt(QPointF(ev.pos())) img_name = self.document().documentLayout().imageAt(QPointF(ev.pos()))
@ -1033,7 +1039,7 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{
align_menu.addAction(QIcon.ic('trash.png'), _('Remove this image')).triggered.connect(partial(self.remove_image_at, c.position())) align_menu.addAction(QIcon.ic('trash.png'), _('Remove this image')).triggered.connect(partial(self.remove_image_at, c.position()))
link_name = self.document().documentLayout().anchorAt(QPointF(ev.pos())) link_name = self.document().documentLayout().anchorAt(QPointF(ev.pos()))
if link_name: if link_name:
menu.addAction(QIcon.ic('insert-link.png'), _('Open link'), partial(safe_open_url, QUrl(link_name))) menu.addAction(QIcon.ic('insert-link.png'), _('Open link'), partial(self.open_link, link_name))
for ac in 'undo redo -- cut copy paste paste_and_match_style -- select_all'.split(): for ac in 'undo redo -- cut copy paste paste_and_match_style -- select_all'.split():
if ac == '--': if ac == '--':
menu.addSeparator() menu.addSeparator()