Skip to content

Commit

Permalink
Fix to let the "insert link" dialog allow anchors
Browse files Browse the repository at this point in the history
Fixes #1932
  • Loading branch information
jaap-karssenberg committed Feb 17, 2022
1 parent 4a78042 commit 458688c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
17 changes: 12 additions & 5 deletions zim/gui/pageview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8188,12 +8188,19 @@ def on_short_link_pref_changed(self, o):
def _link_to_text(self, link):
if not link:
return ''
if self.form['short_links'] and link_type(link) == 'page':
elif self.form['short_links'] and link_type(link) == 'page':
# Similar to 'short_links' notebook property but using uistate instead
parts = HRef.new_from_wiki_link(link).parts()
if len(parts) > 0:
return parts[-1]
return link
try:

This comment has been minimized.

Copy link
@introt

introt Feb 20, 2022

Collaborator

I think we should use HRef.short_name here to avoid re-implementing the same fix in multiple places

This comment has been minimized.

Copy link
@introt

introt Feb 20, 2022

Collaborator

@jaap-karssenberg please see #1942; anchor handling needs to be "centralized" to stop these issues from popping up all over the place

parts = HRef.new_from_wiki_link(link).parts()
except ValueError:
return ''
else:
if len(parts) > 0:
return parts[-1]

This comment has been minimized.

Copy link
@introt

introt Feb 20, 2022

Collaborator

Is the short link for "Home#Heading" supposed to be just "Home", as opposed to "#Heading"/"Heading"?

image

This comment has been minimized.

Copy link
@jaap-karssenberg

jaap-karssenberg via email Feb 22, 2022

Author Member

This comment has been minimized.

Copy link
@introt

introt Feb 22, 2022

Collaborator

@jaap-karssenberg I can fix the comments on #1560 since @roland-ruedenauer is MIA and we can discuss API details later.

Some clarification around the intended behavior here would be welcome: is the "short link name for pages" for the floating link "Home#Heading" supposed to be the page name, anchor name or both? I find both page name and anchor without # (unless on the same page) reasonable, so I would like to hear your opinion.

This comment has been minimized.

Copy link
@introt

introt Feb 22, 2022

Collaborator

Fixed in #1948, it makes most sense if the "short link names for pages" only affects the link name for pages and leaves the anchor untouched.

else:
return link
else:
return link

def do_response_ok(self):
self.uistate['short_links'] = self.form['short_links']
Expand Down
23 changes: 11 additions & 12 deletions zim/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
from zim.fs import adapt_from_oldfs
from zim.newfs import FilePath, LocalFile, LocalFolder
from zim.config import value_is_coord
from zim.notebook import Notebook, Path, PageNotFoundError
from zim.notebook import Notebook, Path, HRef, PageNotFoundError
from zim.parsing import link_type
from zim.signals import ConnectorMixin
from zim.notebook.index import IndexNotFoundError
Expand Down Expand Up @@ -1982,24 +1982,23 @@ def __init__(self, notebook, path=None):
self.file_type_hint = None

def get_path(self):
# Check we actually got a valid path
text = self.get_text()
if text:
type = link_type(text)
if type == 'page':
return PageEntry.get_path(self)
else:
return None
else:
return None
# TODO: proper check link syntax, including achor part instead
# of just using PageEntry.get_path()
raise NotImplementedError

def update_input_valid(self):
# Switch between path completion and file completion
text = self.get_text()
if text:
type = link_type(text)
if type == 'page':
PageEntry.update_input_valid(self)
try:
href = HRef.new_from_wiki_link(text)
except ValueError:
self.set_input_valid(True)
else:
self.set_input_valid(href.names or href.anchor)
# Should not reduce to empty link
#~ elif type == 'file':
#~ FileEntry.update_input_valid(self)
else:
Expand Down

0 comments on commit 458688c

Please sign in to comment.