Skip to content

Commit

Permalink
New Root URL: Fix suggested Name to be more reasonable
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfstr committed Jan 4, 2024
1 parent 0f8af86 commit ec70f7c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/crystal/browser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ def _suggested_url_or_url_pattern_for_selection(self) -> Optional[str]:
def _suggested_source_for_selection(self) -> Optional[ResourceGroupSource]:
return self.entity_tree.source_of_selected_entity

@property
def _suggested_name_for_selection(self) -> Optional[str]:
return self.entity_tree.name_of_selected_entity

# === Operations ===

def close(self) -> None:
Expand Down Expand Up @@ -427,6 +431,7 @@ def root_url_exists(url: str) -> bool:
self._frame, self._on_add_url_dialog_ok,
url_exists_func=root_url_exists,
initial_url=self._suggested_url_or_url_pattern_for_selection or '',
initial_name=self._suggested_name_for_selection or '',
)

@fg_affinity
Expand All @@ -444,7 +449,8 @@ def _on_add_group(self, event: wx.CommandEvent) -> None:
self._frame, self._on_add_group_dialog_ok,
self.project,
initial_url_pattern=self._suggested_url_or_url_pattern_for_selection or '',
initial_source=self._suggested_source_for_selection)
initial_source=self._suggested_source_for_selection,
initial_name=self._suggested_name_for_selection or '')
except CancelLoadUrls:
pass

Expand Down
9 changes: 6 additions & 3 deletions src/crystal/browser/addgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self,
project: Project,
initial_url_pattern: str='',
initial_source: Optional[ResourceGroupSource]=None,
initial_name: str='',
) -> None:
"""
Arguments:
Expand All @@ -36,6 +37,7 @@ def __init__(self,
* project -- the project.
* initial_url_pattern -- overrides the initial URL pattern displayed.
* initial_source -- overrides the initial source displayed.
* initial_name -- overrides the initial name displayed.
Raises:
* CancelLoadUrls
Expand Down Expand Up @@ -92,7 +94,7 @@ def __init__(self,

content_sizer = wx.BoxSizer(wx.VERTICAL)
content_sizer.Add(
self._create_fields(dialog, initial_url_pattern, initial_source),
self._create_fields(dialog, initial_url_pattern, initial_source, initial_name),
flag=wx.EXPAND)
content_sizer.Add(preview_box, proportion=1, flag=wx.EXPAND|preview_box_flags, border=preview_box_border)

Expand All @@ -115,7 +117,8 @@ def __init__(self,
def _create_fields(self,
parent: wx.Window,
initial_url_pattern: str,
initial_source: Optional[ResourceGroupSource]
initial_source: Optional[ResourceGroupSource],
initial_name: str,
) -> wx.Sizer:
fields_sizer = wx.FlexGridSizer(cols=2,
vgap=_FORM_ROW_SPACING, hgap=_FORM_LABEL_INPUT_SPACING)
Expand Down Expand Up @@ -157,7 +160,7 @@ def _create_fields(self,

fields_sizer.Add(wx.StaticText(parent, label='Name:', style=wx.ALIGN_RIGHT), flag=wx.EXPAND)
self.name_field = wx.TextCtrl(
parent,
parent, value=initial_name,
name='cr-add-group-dialog__name-field')
self.name_field.Hint = 'Post'
self.name_field.SetSelection(-1, -1) # select all upon focus
Expand Down
7 changes: 4 additions & 3 deletions src/crystal/browser/addrooturl.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self,
on_finish: Callable[[str, str], None],
url_exists_func: Callable[[str], bool],
initial_url: str='',
initial_name: str='',
) -> None:
"""
Arguments:
Expand All @@ -52,7 +53,7 @@ def __init__(self,
bind(dialog, wx.EVT_CLOSE, self._on_close)
bind(dialog, wx.EVT_WINDOW_DESTROY, self._on_destroyed)

dialog_sizer.Add(self._create_fields(dialog, initial_url), flag=wx.EXPAND|wx.ALL,
dialog_sizer.Add(self._create_fields(dialog, initial_url, initial_name), flag=wx.EXPAND|wx.ALL,
border=_WINDOW_INNER_PADDING)
dialog_sizer.Add(dialog.CreateButtonSizer(wx.OK|wx.CANCEL), flag=wx.EXPAND|wx.BOTTOM,
border=_WINDOW_INNER_PADDING)
Expand All @@ -75,7 +76,7 @@ def __init__(self,
if os.environ.get('CRYSTAL_RUNNING_TESTS', 'False') == 'True':
AddRootUrlDialog._last_opened = self

def _create_fields(self, parent: wx.Window, initial_url: str) -> wx.Sizer:
def _create_fields(self, parent: wx.Window, initial_url: str, initial_name: str) -> wx.Sizer:
fields_sizer = wx.FlexGridSizer(rows=2, cols=2,
vgap=_FORM_ROW_SPACING, hgap=_FORM_LABEL_INPUT_SPACING)
fields_sizer.AddGrowableCol(1)
Expand Down Expand Up @@ -115,7 +116,7 @@ def _create_fields(self, parent: wx.Window, initial_url: str) -> wx.Sizer:
name_field_and_space = wx.BoxSizer(wx.HORIZONTAL)
if True:
self.name_field = wx.TextCtrl(
parent,
parent, value=initial_name,
name='cr-add-url-dialog__name-field')
self.name_field.Hint = 'Home'
self.name_field.SetSelection(-1, -1) # select all upon focus
Expand Down
23 changes: 23 additions & 0 deletions src/crystal/browser/entitytree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from crystal.browser.icons import BADGED_TREE_NODE_ICON, TREE_NODE_ICONS
from crystal.doc.generic import Link
from crystal.doc.html.soup import TEXT_LINK_TYPE_TITLE
from crystal.model import (
Project, ProjectHasTooManyRevisionsError, Resource, ResourceGroup,
ResourceGroupSource,
Expand Down Expand Up @@ -102,6 +103,28 @@ def source_of_selected_entity(self) -> Optional[ResourceGroupSource]:
ancestor_node_view = self._parent_of_node_view(ancestor_node_view)
return None

@property
def name_of_selected_entity(self) -> Optional[str]:
entity = self.selected_entity
if isinstance(entity, (RootResource, ResourceGroup)):
return entity.name
elif isinstance(entity, Resource):
selected_node_view = self.view.selected_node
if selected_node_view is None:
return None
selected_node = self._node_for_node_view(selected_node_view)
if isinstance(selected_node, LinkedResourceNode):
for link in selected_node.links:
if (link.type_title == TEXT_LINK_TYPE_TITLE and
link.title is not None and
link.title != ''):
return link.title
return None
else:
return None
else:
return None

@staticmethod
def _node_for_node_view(node_view: NodeView) -> Node:
node = node_view.delegate
Expand Down
5 changes: 4 additions & 1 deletion src/crystal/doc/html/soup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class _XPaths:
) for T in _PARSER_LIBRARY_T_CHOICES}


TEXT_LINK_TYPE_TITLE = 'Link'


def parse_html_and_links(
html_bytes: bytes,
declared_charset: Optional[str],
Expand Down Expand Up @@ -119,7 +122,7 @@ def parse_html_and_links(
embedded = False
if tag_name == 'a':
title = html.tag_string(tag)
type_title = 'Link'
type_title = TEXT_LINK_TYPE_TITLE # 'Link'
elif tag_name == 'link' and (
('rel' in tag_attrs and 'stylesheet' in tag_attrs['rel']) or (
'type' in tag_attrs and tag_attrs['type'] == 'text/css') or (
Expand Down

0 comments on commit ec70f7c

Please sign in to comment.