From 3d4b96309624f07ac3bd9d2f23ca0971792a7e25 Mon Sep 17 00:00:00 2001 From: Ivan Fonseca Date: Mon, 23 Jul 2018 21:06:48 -0400 Subject: [PATCH] Open in new tabs: Add support for links in text Adds a new sub-option to the "Open links in new tabs" setting which uses javascript to find external links in the text of topics, comments, and messages and sets them to open in new tabs. --- ..._add_setting_to_open_text_links_in_new_.py | 24 +++++++++++++++++++ .../js/behaviors/external-links-new-tabs.js | 8 +++++++ tildes/tildes/models/user/user.py | 2 ++ .../tildes/templates/macros/comments.jinja2 | 6 ++++- .../tildes/templates/macros/messages.jinja2 | 6 ++++- tildes/tildes/templates/macros/topics.jinja2 | 6 ++++- tildes/tildes/templates/settings.jinja2 | 14 +++++++++++ tildes/tildes/templates/topic.jinja2 | 6 ++++- tildes/tildes/views/api/web/user.py | 2 ++ 9 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tildes/alembic/versions/de83b8750123_add_setting_to_open_text_links_in_new_.py create mode 100644 tildes/static/js/behaviors/external-links-new-tabs.js diff --git a/tildes/alembic/versions/de83b8750123_add_setting_to_open_text_links_in_new_.py b/tildes/alembic/versions/de83b8750123_add_setting_to_open_text_links_in_new_.py new file mode 100644 index 0000000..e8705db --- /dev/null +++ b/tildes/alembic/versions/de83b8750123_add_setting_to_open_text_links_in_new_.py @@ -0,0 +1,24 @@ +"""Add setting to open text links in new tab + +Revision ID: de83b8750123 +Revises: 2512581c91b3 +Create Date: 2018-07-24 03:10:59.485645 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'de83b8750123' +down_revision = '2512581c91b3' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('users', sa.Column('open_new_tab_text', sa.Boolean(), server_default='false', nullable=False)) + + +def downgrade(): + op.drop_column('users', 'open_new_tab_text') diff --git a/tildes/static/js/behaviors/external-links-new-tabs.js b/tildes/static/js/behaviors/external-links-new-tabs.js new file mode 100644 index 0000000..20b90f3 --- /dev/null +++ b/tildes/static/js/behaviors/external-links-new-tabs.js @@ -0,0 +1,8 @@ +$.onmount('[data-js-external-links-new-tabs]', function() { + // Open external links in topic, comment, and message text in new tabs + $(this).find('a').each(function() { + if (this.host !== window.location.host) { + $(this).attr('target', '_blank'); + } + }); +}); diff --git a/tildes/tildes/models/user/user.py b/tildes/tildes/models/user/user.py index d4f9f40..af69bf7 100644 --- a/tildes/tildes/models/user/user.py +++ b/tildes/tildes/models/user/user.py @@ -86,6 +86,8 @@ class User(DatabaseModel): Boolean, nullable=False, server_default='false') open_new_tab_internal: bool = Column( Boolean, nullable=False, server_default='false') + open_new_tab_text: bool = Column( + Boolean, nullable=False, server_default='false') is_banned: bool = Column(Boolean, nullable=False, server_default='false') is_admin: bool = Column(Boolean, nullable=False, server_default='false') home_default_order: Optional[TopicSortOption] = Column( diff --git a/tildes/tildes/templates/macros/comments.jinja2 b/tildes/tildes/templates/macros/comments.jinja2 index 8fdec2e..4d9b147 100644 --- a/tildes/tildes/templates/macros/comments.jinja2 +++ b/tildes/tildes/templates/macros/comments.jinja2 @@ -94,7 +94,11 @@ {% endif %} -
+
{% if comment.is_removed and 'admin' in request.effective_principals %}

Comment removed

{% endif %} diff --git a/tildes/tildes/templates/macros/messages.jinja2 b/tildes/tildes/templates/macros/messages.jinja2 index f7e8e84..5f95bfa 100644 --- a/tildes/tildes/templates/macros/messages.jinja2 +++ b/tildes/tildes/templates/macros/messages.jinja2 @@ -13,6 +13,10 @@ {{ time_ago(message.created_time) }} -
{{ message.rendered_html|safe }}
+
{{ message.rendered_html|safe }}
{% endmacro %} diff --git a/tildes/tildes/templates/macros/topics.jinja2 b/tildes/tildes/templates/macros/topics.jinja2 index daa1c06..eb9a5b8 100644 --- a/tildes/tildes/templates/macros/topics.jinja2 +++ b/tildes/tildes/templates/macros/topics.jinja2 @@ -51,7 +51,11 @@ {% if not topic.get_content_metadata('excerpt').endswith('...') %}

{{ topic.get_content_metadata('excerpt') }}

{% else %} -
+
{{ topic.get_content_metadata('excerpt') }} diff --git a/tildes/tildes/templates/settings.jinja2 b/tildes/tildes/templates/settings.jinja2 index e8264f6..bd16d7e 100644 --- a/tildes/tildes/templates/settings.jinja2 +++ b/tildes/tildes/templates/settings.jinja2 @@ -73,6 +73,20 @@
+
  • +
    + +
    +
  • diff --git a/tildes/tildes/templates/topic.jinja2 b/tildes/tildes/templates/topic.jinja2 index 508f15e..78e60a8 100644 --- a/tildes/tildes/templates/topic.jinja2 +++ b/tildes/tildes/templates/topic.jinja2 @@ -40,7 +40,11 @@ {% if request.has_permission('view_content', topic) %} {% if topic.is_text_type %} -
    {{ topic.rendered_html|safe }}
    +
    {{ topic.rendered_html|safe }}
    {% elif topic.is_link_type %}