-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Improve support for snippets throughout Winter CMS #36
Draft
LukeTowers
wants to merge
21
commits into
main
Choose a base branch
from
wip/snippet-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f49e9a8
Initial work on implementing a RichEditor snippets button
LukeTowers a179118
- Add SnippetLoader class
mjauvin c8852f2
Add parseSnippets twig filter
mjauvin ff8a10b
document why some code was commented out
mjauvin f8ef7ac
fix namespace
mjauvin 71a9fc9
replace self:: with static::
mjauvin 6a8c6f5
remove error squelching
mjauvin 56ee40a
Add type hints
LukeTowers 71cc21d
Add typehints
LukeTowers f80cf61
Merge pull request #28 from wintercms/wip/snippet-parser
LukeTowers 3b68d59
Apply suggestions from code review
LukeTowers c55327a
Merge branch 'main' into wip/snippet-button
LukeTowers 7db20dc
Update Plugin.php
LukeTowers a98fe1d
Update Plugin.php
LukeTowers fabd5bd
Update Plugin.php
LukeTowers 994d119
Missing comma
LukeTowers 202ad76
Update classes/SnippetLoader.php
LukeTowers 417d42d
Test on 8.2
LukeTowers bb22781
add reason for thrown exceptions; remove CmsException
mjauvin be2c626
remove OC2 changes
mjauvin 8aeb4b1
improve comment about snippet code
mjauvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
(function ($) { | ||
// Adds snippets to the default Froala buttons | ||
// Places it after the quote button to keep dropdowns together | ||
$.wn.richEditorButtons.splice(3, 0, 'snippets'); | ||
|
||
// Define a dropdown button. | ||
$.FroalaEditor.RegisterCommand('snippets', { | ||
// Button title. | ||
title: 'Snippets', | ||
|
||
// Mark the button as a dropdown. | ||
type: 'dropdown', | ||
|
||
// Specify the icon for the button. | ||
// If this option is not specified, the button name will be used. | ||
icon: '<i class="icon-newspaper-o"></i>', | ||
|
||
// The dropdown HTML | ||
html: function() { | ||
if (!$.wn.snippets) { | ||
return '<div style="padding:10px;">No snippets are currently defined.</div>'; | ||
} | ||
|
||
var html = '<ul class="fr-dropdown-list">'; | ||
|
||
$.each($.wn.snippets, function(i, snippet) { | ||
html += '<li><a class="fr-command" data-cmd="snippets" data-param1="' + snippet.snippet + '" title="' + snippet.name + '">' + snippet.name + '</a></li>'; | ||
}); | ||
|
||
return html + '</ul>'; | ||
}, | ||
|
||
// Save the dropdown action into undo stack. | ||
undo: true, | ||
|
||
// Focus inside the editor before callback. | ||
focus: true, | ||
|
||
// Refresh the button state after the callback. | ||
refreshAfterCallback: true, | ||
|
||
// Callback. | ||
callback: function (cmd, val, params) { | ||
var options = $.wn.snippets[val]; | ||
|
||
if (options) { | ||
// Get editor element. OC2's richeditor has 2 nested data-control=richeditor, we want the outer one | ||
var $editor = this.$el.parents('[data-control="richeditor"]:not([data-richeditor-vue])'); | ||
mjauvin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var $snippetNode = $('<figure contenteditable="false" data-inspector-css-class="hero"> </figure>'); | ||
|
||
if (options.component) { | ||
$snippetNode.attr({ | ||
'data-component': options.component, | ||
'data-inspector-class': options.component | ||
}) | ||
|
||
// If a component-based snippet was added, make sure that | ||
// its code is unique, as it will be used as a component | ||
// alias. | ||
|
||
/* | ||
// Init a new snippet manager | ||
|
||
// Below code reattaches the inspector event, causing duplicate inspector options | ||
// Until I can figure a solution, I have copied the code to this file... | ||
|
||
var snippetManager = new $.wn.pages.snippetManager; | ||
options.snippet = snippetManager.generateUniqueComponentSnippetCode(options.component, options.snippet, $editor.parent()) | ||
*/ | ||
|
||
options.snippet = generateUniqueComponentSnippetCode(options.component, options.snippet, $editor.parent()); | ||
} | ||
|
||
$snippetNode.attr({ | ||
'data-snippet': options.snippet, | ||
'data-name': options.name, | ||
'tabindex': '0', | ||
'draggable': 'true', | ||
'data-ui-block': 'true' | ||
}) | ||
|
||
$snippetNode.addClass('fr-draggable'); | ||
|
||
// Insert the content | ||
this.figures.insert($snippetNode); | ||
} | ||
|
||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}); | ||
|
||
generateUniqueComponentSnippetCode = function(componentClass, originalCode, $pageForm) { | ||
var updatedCode = originalCode, | ||
counter = 1, | ||
snippetFound = false | ||
|
||
do { | ||
snippetFound = false | ||
|
||
$('[data-control="richeditor"] textarea', $pageForm).each(function() { | ||
var $textarea = $(this), | ||
$codeDom = $('<div>' + $textarea.val() + '</div>') | ||
|
||
if ($codeDom.find('[data-snippet="'+updatedCode+'"][data-component]').length > 0) { | ||
snippetFound = true | ||
updatedCode = originalCode + counter | ||
counter++ | ||
|
||
return false | ||
} | ||
}) | ||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} while (snippetFound) | ||
|
||
return updatedCode | ||
}; | ||
|
||
|
||
/** | ||
* Because the pages-snippets.js is injected after the richeditor script, it will register its | ||
* initialization hooks too late. Here we need to force initialization in order to work with forms | ||
* that are displayed on page load (i.e. Winter.Blog). | ||
*/ | ||
$(document).ready(function() { | ||
var $editor = $('[data-control="richeditor"]:not([data-richeditor-vue])'); | ||
|
||
if ($.wn.pagesPage && !window.location.pathname.includes('winter/pages')) { | ||
$.wn.pagesPage.snippetManager.initSnippets($editor); | ||
} | ||
}); | ||
|
||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
})(jQuery); | ||
LukeTowers marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need documentation for this filter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LukeTowers I think we should use a modified version of the original
readme.md
fileref. https://github.com/inetis-ch/oc-richeditorsnippets-plugin
Do you have a suggestion on where to integrate this content ?