Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto save editor when in living mode #215

Merged
merged 5 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ MARTOR_IMGUR_API_KEY = 'your-api-key'
MARTOR_MARKDOWNIFY_FUNCTION = 'martor.utils.markdownify' # default
MARTOR_MARKDOWNIFY_URL = '/martor/markdownify/' # default

# Delay in miliseconds to update editor preview when in living mode.
MARTOR_MARKDOWNIFY_TIMEOUT = 0 # update the preview instantly
# or:
MARTOR_MARKDOWNIFY_TIMEOUT = 1000 # default

# Markdown extensions (default)
MARTOR_MARKDOWN_EXTENSIONS = [
'markdown.extensions.extra',
Expand Down
3 changes: 3 additions & 0 deletions martor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
settings, "MARTOR_MARKDOWNIFY_URL", "/martor/markdownify/"
)

# Time to delay the markdownify ajax request, in millisecond.
MARTOR_MARKDOWNIFY_TIMEOUT = getattr(settings, "MARTOR_MARKDOWNIFY_TIMEOUT", 1000)

# Markdown extensions
MARTOR_MARKDOWN_EXTENSIONS = getattr(
settings,
Expand Down
11 changes: 10 additions & 1 deletion martor/static/martor/js/martor.bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@
});
};

let timeoutID;

var refreshPreviewTimeout = function () {
if (timeoutID) {
clearTimeout(timeoutID);
}
timeoutID = setTimeout(refreshPreview, textareaId.data("save-timeout"));
}

// Refresh the preview unconditionally on first load.
window.onload = function () {
refreshPreview();
Expand All @@ -202,7 +211,7 @@
refreshPreview();
});
} else {
editor.on('change', refreshPreview);
editor.on('change', refreshPreviewTimeout);
}

if (editorConfig.spellcheck == 'true') {
Expand Down
2 changes: 1 addition & 1 deletion martor/static/martor/js/martor.bootstrap.min.js

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion martor/static/martor/js/martor.semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@
});
};

let timeoutID;

var refreshPreviewTimeout = function () {
if (timeoutID) {
clearTimeout(timeoutID);
}
timeoutID = setTimeout(refreshPreview, textareaId.data("save-timeout"));
}

// Refresh the preview unconditionally on first load.
window.onload = function () {
refreshPreview();
Expand All @@ -191,7 +200,7 @@
refreshPreview();
});
} else {
editor.on('change', refreshPreview);
editor.on('change', refreshPreviewTimeout);
}

var editorTabButton = $('.item[data-tab=editor-tab-' + field_name + ']');
Expand Down
2 changes: 1 addition & 1 deletion martor/static/martor/js/martor.semantic.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions martor/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
MARTOR_ALTERNATIVE_JS_FILE_THEME,
MARTOR_ALTERNATIVE_CSS_FILE_THEME,
MARTOR_ALTERNATIVE_JQUERY_JS_FILE,
MARTOR_MARKDOWNIFY_TIMEOUT
)


Expand All @@ -41,6 +42,8 @@ def render(self, name, value, attrs=None, renderer=None, **kwargs):
attributes_to_pass["data-search-users-url"] = reverse("search_user_json")
if MARTOR_SEARCH_USERS_URL:
attributes_to_pass["data-base-emoji-url"] = MARTOR_MARKDOWN_BASE_EMOJI_URL
if MARTOR_MARKDOWNIFY_TIMEOUT:
attributes_to_pass["data-save-timeout"] = MARTOR_MARKDOWNIFY_TIMEOUT

# Make sure that the martor value is in the class attr passed in
if "class" in attrs:
Expand Down
Loading