Link preview working.

This commit is contained in:
lwark
2025-09-17 10:03:23 -05:00
parent a2ff72dda8
commit c18d84c3fb
7 changed files with 81 additions and 4 deletions

View File

@@ -234,6 +234,9 @@ class EditForm(forms.Form, SpamProtectionMixin):
title = forms.CharField( title = forms.CharField(
label=_("Title"), label=_("Title"),
) )
# preview_text = forms.CharField(
# label=_("Preview Text"),
# )
content = forms.CharField( content = forms.CharField(
label=_("Contents"), required=False label=_("Contents"), required=False
) # @UndefinedVariable ) # @UndefinedVariable
@@ -390,6 +393,9 @@ class CreateForm(forms.Form, SpamProtectionMixin):
), ),
max_length=models.URLPath.SLUG_MAX_LENGTH, max_length=models.URLPath.SLUG_MAX_LENGTH,
) )
# preview_text = forms.CharField(
# label=_("Preview Text"),
# )
content = forms.CharField( content = forms.CharField(
label=_("Contents"), required=False, widget=getEditor().get_widget() label=_("Contents"), required=False, widget=getEditor().get_widget()
) # @UndefinedVariable ) # @UndefinedVariable

View File

@@ -415,6 +415,17 @@ class ArticleRevision(BaseRevisionMixin, models.Model):
), ),
) )
# preview_text = models.CharField(
# max_length=1024,
# verbose_name=_("preview text"),
# null=False,
# blank=False,
# default="",
# help_text=(
# "Please fill out the preview text."
# )
# )
# TODO: # TODO:
# Allow a revision to redirect to another *article*. This # Allow a revision to redirect to another *article*. This
# way, we can have redirects and still maintain old content. # way, we can have redirects and still maintain old content.

View File

@@ -0,0 +1,53 @@
/**
* Creates a link preview of a specific div from the target URL when a link is hovered.
* @param {string} linkSelector - CSS selector for the links to preview.
* @param {string} divSelector - CSS selector for the div to extract from the target page.
*/
function setupLinkPreview(linkSelector, divSelector) {
const preview = document.createElement('div');
preview.style.position = 'absolute';
preview.style.border = '1px solid #ccc';
preview.style.width = '400px';
preview.style.height = '300px';
preview.style.overflow = 'auto';
preview.style.zIndex = '9999';
preview.style.display = 'none';
preview.style.backgroundColor = '#fff';
preview.style.boxShadow = '0 0 10px rgba(0,0,0,0.2)';
preview.style.padding = '10px';
document.body.appendChild(preview);
const links = document.querySelectorAll(linkSelector);
links.forEach(link => {
link.addEventListener('mouseover', async (e) => {
try {
const response = await fetch(link.href);
const html = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const targetDiv = doc.querySelector(divSelector);
if (targetDiv) {
preview.innerHTML = targetDiv.innerHTML;
preview.style.display = 'block';
preview.style.left = `${e.pageX + 10}px`;
preview.style.top = `${e.pageY + 10}px`;
} else {
preview.innerHTML = '<p>Target div not found.</p>';
preview.style.display = 'block';
}
} catch (error) {
preview.innerHTML = '<p>Failed to load preview.</p>';
preview.style.display = 'block';
}
});
link.addEventListener('mouseout', () => {
preview.style.display = 'none';
preview.innerHTML = '';
});
});
}
// Example usage:
// Call this function after the DOM is loaded.
// setupLinkPreview('a.external-link', '#target-div');

View File

@@ -1,6 +1,5 @@
{% extends "wiki/base.html" %} {% extends "wiki/base.html" %}
{% load wiki_tags i18n sekizai_tags %} {% load wiki_tags i18n sekizai_tags static %}
{% block wiki_pagetitle %}{{ article.current_revision.title }}{% endblock %} {% block wiki_pagetitle %}{{ article.current_revision.title }}{% endblock %}
@@ -9,7 +8,7 @@
{% endblock %} {% endblock %}
{% block wiki_contents %} {% block wiki_contents %}
<!-- article.html -->
<div id="article-container"> <div id="article-container">
<nav id="article-menu" class="navbar navbar-expand-md nav-pills"> <nav id="article-menu" class="navbar navbar-expand-md nav-pills">
<ul class="navbar-nav w-75"> <ul class="navbar-nav w-75">

View File

@@ -121,6 +121,8 @@
<!-- Reserved for breadcrumbs --> <!-- Reserved for breadcrumbs -->
{% block wiki_breadcrumbs %}{% endblock %} {% block wiki_breadcrumbs %}{% endblock %}
<!-- base_site.html -->
<!-- Main page contents go here --> <!-- Main page contents go here -->
{% block wiki_contents %}{% endblock %} {% block wiki_contents %}{% endblock %}
@@ -146,6 +148,10 @@
<script src="{% static "wiki/bootstrap/js/bootstrap.bundle.min.js" %}"></script> <script src="{% static "wiki/bootstrap/js/bootstrap.bundle.min.js" %}"></script>
<script src="{% static "wiki/js/respond.min.js" %}"></script> <script src="{% static "wiki/js/respond.min.js" %}"></script>
<script src="{% static "wiki/js/previewlink.js" %}"></script>
<script>
window.addEventListener("DOMContentLoaded", setupLinkPreview('a.internal-link', '.wiki-article'), false);
</script>
{% render_block "js" %} {% render_block "js" %}
</body> </body>

View File

@@ -1,7 +1,7 @@
{% extends "wiki/article.html" %} {% extends "wiki/article.html" %}
{% load wiki_tags i18n sekizai_tags %} {% load wiki_tags i18n sekizai_tags %}
<!-- view.html -->
{% block wiki_contents_tab %} {% block wiki_contents_tab %}
{% wiki_render article %} {% wiki_render article %}

View File

@@ -27,6 +27,8 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
# NOTE: allow frame links for the same base url
X_FRAME_OPTIONS = 'SAMEORIGIN'
# Application definition # Application definition