Track parent page for internal links in rich text - fixes #2639

This commit is contained in:
Matt Westcott 2016-06-03 18:53:52 +01:00 committed by Mikalai Radchuk
parent 978cef5218
commit aacb1116db
7 changed files with 26 additions and 8 deletions

View file

@ -16,6 +16,7 @@ Changelog
* Fix: Rich text editor is no longer broken in InlinePanels (Matt Westcott, Yann Fouillat)
* Fix: Rich text editor is no longer broken in settings (Matt Westcott)
* Fix: Link tooltip now shows correct urls for newly inserted document links (Matt Westcott)
* Fix: Now page chooser (in a rich text editor) opens up at the link's parent page, rather than at the page itself (Matt Westcott)
1.5 (31.05.2016)

View file

@ -19,3 +19,4 @@ Bug fixes
* Rich text editor is no longer broken in InlinePanels (Matt Westcott, Yann Fouillat)
* Rich text editor is no longer broken in settings (Matt Westcott)
* Link tooltip now shows correct urls for newly inserted document links (Matt Westcott)
* Now page chooser (in a rich text editor) opens up at the link's parent page, rather than at the page itself (Matt Westcott)

View file

@ -45,14 +45,13 @@
if (enclosingLink) {
href = enclosingLink.getAttribute('href');
pageId = enclosingLink.getAttribute('data-id');
parentPageId = enclosingLink.getAttribute('data-parent-id');
linkType = enclosingLink.getAttribute('data-linktype');
urlParams['link_text'] = enclosingLink.innerText;
if (linkType == 'page' && pageId) {
// TODO: Actually show the parent not the page itself.
url = window.chooserUrls.pageChooser + pageId.toString() + '/';
if (linkType == 'page' && parentPageId) {
url = window.chooserUrls.pageChooser + parentPageId.toString() + '/';
} else if (href.startsWith('mailto:')) {
url = window.chooserUrls.emailLinkChooser;
href = href.replace('mailto:', '');
@ -77,6 +76,7 @@
a.setAttribute('href', pageData.url);
if (pageData.id) {
a.setAttribute('data-id', pageData.id);
a.setAttribute('data-parent-id', pageData.parentId);
a.setAttribute('data-linktype', 'page');
}

View file

@ -6,7 +6,7 @@ Expects a variable 'page', the page instance.
<h2>
{% if page.can_choose %}
<a class="choose-page" href="#{{ page.id }}" data-id="{{ page.id }}" data-title="{{ page.title }}" data-url="{{ page.url }}" data-edit-url="{% url 'wagtailadmin_pages:edit' page.id %}">{{ page.title }}</a>
<a class="choose-page" href="#{{ page.id }}" data-id="{{ page.id }}" data-title="{{ page.title }}" data-url="{{ page.url }}" data-parent-id="{{ page.get_parent.id }}" data-edit-url="{% url 'wagtailadmin_pages:edit' page.id %}">{{ page.title }}</a>
{% else %}
{{ page.title }}
{% endif %}

View file

@ -41,6 +41,9 @@ class PageLinkHandler(object):
if for_editor:
editor_attrs = 'data-linktype="page" data-id="%d" ' % page.id
parent_page = page.get_parent()
if parent_page:
editor_attrs += 'data-parent-id="%d" ' % parent_page.id
else:
editor_attrs = ''

View file

@ -193,7 +193,7 @@ class TestRichTextBlock(TestCase):
self.assertIn(
(
'&lt;p&gt;Merry &lt;a data-linktype=&quot;page&quot; data-id=&quot;4&quot;'
' href=&quot;/events/christmas/&quot;&gt;Christmas&lt;/a&gt;!&lt;/p&gt;'
' data-parent-id=&quot;3&quot; href=&quot;/events/christmas/&quot;&gt;Christmas&lt;/a&gt;!&lt;/p&gt;'
),
result
)

View file

@ -4,6 +4,7 @@ from bs4 import BeautifulSoup
from django.test import TestCase
from mock import patch
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.rich_text import (
DbWhitelister, PageLinkHandler, RichText, expand_db_html, extract_attrs)
@ -30,8 +31,20 @@ class TestPageLinkHandler(TestCase):
{'id': 1},
True
)
self.assertEqual(result,
'<a data-linktype="page" data-id="1" href="None">')
self.assertEqual(
result,
'<a data-linktype="page" data-id="1" href="None">'
)
events_page_id = Page.objects.get(url_path='/home/events/').pk
result = PageLinkHandler.expand_db_attributes(
{'id': events_page_id},
True
)
self.assertEqual(
result,
'<a data-linktype="page" data-id="%d" data-parent-id="2" href="/events/">' % events_page_id
)
def test_expand_db_attributes_not_for_editor(self):
result = PageLinkHandler.expand_db_attributes(