yesod-form-richtext (empty) → 0.1.0.0
raw patch · 4 files changed
+172/−0 lines, 4 filesdep +basedep +blaze-builderdep +blaze-htmlsetup-changed
Dependencies added: base, blaze-builder, blaze-html, shakespeare, text, xss-sanitize, yesod-core, yesod-form
Files
- LICENSE +18/−0
- Setup.hs +2/−0
- src/Yesod/Form/Summernote.hs +117/−0
- yesod-form-richtext.cabal +35/−0
+ LICENSE view
@@ -0,0 +1,18 @@+Copyright © 2015-2016 Arthur S. Fayzrakhmanov <https://github.com/geraldus>++Permission is hereby granted, free of charge, to any person obtaining a copy of+this software and associated documentation files (the "Software"), to deal in+the Software without restriction, including without limitation the rights to+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of+the Software, and to permit persons to whom the Software is furnished to do so,+subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Yesod/Form/Summernote.hs view
@@ -0,0 +1,117 @@+-- | Provide the user with the Summernote rich text editor.+--+-- /NOTES:/+--+-- Editor fields have hidden textareas which are updated automatically when+-- editor contents changes.+--+-- @+-- summerForm :: Form HtmlComment+-- summerForm = renderBootstrap3 BootstrapBasicForm $ HtmlComment+-- \<$\> areq (snHtmlFieldCustomized "{toolbar:false}") "Title" Nothing+-- \<*\> areq snHtmlField "Comment" Nothing+-- @++module Yesod.Form.Summernote+ ( YesodSummernote (..)+ , snHtmlField+ , snHtmlFieldCustomized+ ) where++import Control.Monad (when)+import Data.Maybe (listToMaybe)+import Data.Text (Text, pack)+import Text.Blaze.Html.Renderer.String (renderHtml)+import Text.Hamlet (shamlet)+import Text.HTML.SanitizeXSS (sanitizeBalance)+import Text.Julius (julius, rawJS)+import Yesod.Core+import Yesod.Form++class Yesod a => YesodSummernote a where+ -- | Bootstrap 3 CSS location.+ urlBootstrapCss :: a -> Either (Route a) Text+ urlBootstrapCss _ =+ Right "http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css"+ -- | Bootstrap 3 library location.+ urlBootstrapScript :: a -> Either (Route a) Text+ urlBootstrapScript _ =+ Right "http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"+ -- | JQuery library location.+ urlJQueryScript :: a -> Either (Route a) Text+ urlJQueryScript _ =+ Right "http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"+ -- | Summernote Editor CSS location.+ urlSummernoteCss :: a -> Either (Route a) Text+ urlSummernoteCss _ = Right+ "http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.0/summernote.css"+ -- | Summernote Editor library location.+ urlSummernoteScript :: a -> Either (Route a) Text+ urlSummernoteScript _ = Right+ "http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.0/summernote.js"+ -- | Should required libraries and scripts be added in DOM tree? This+ -- property required to control script loading. In case if you load JQuery,+ -- Bootstrap, and Summernote libraries and CSS in @<head>@ it is not+ -- necessary to load them second time, moreover this could bring some+ -- issues, for example if JQuery is loaded second time it could brake AJAX+ -- configuration from 'defaultCsrfMiddleware'. Setting this to @True@ could+ -- be useful in case you need single instance of editor on some pages only.+ summernoteLoadLibrariesAndCss :: a -> Bool+ summernoteLoadLibrariesAndCss _ = False++-- | Customizable Summernote editor field.+--+-- @cfg@ argument should be a JSON formatted string, it will be passed to+-- @$.summernote()@ call as first argument.+--+-- @+-- snHtmlFieldCustomized "{ height: 150, codemirror: { theme:'monokai' } }"+-- @+snHtmlFieldCustomized :: YesodSummernote site+ => String -> Field (HandlerT site IO) Html+snHtmlFieldCustomized cfg = Field+ { fieldParse =+ \e _ -> return $+ Right . fmap (preEscapedToMarkup . sanitizeBalance) . listToMaybe $ e+ , fieldView = \theId name attrs val _isReq -> do+ toWidget [shamlet|+$newline never+<textarea id="#{theId}" *{attrs} name="#{name}" .html>#{showVal val}+|]+ master <- getYesod+ (when (summernoteLoadLibrariesAndCss master) $ do+ addScript' urlJQueryScript+ addStylesheet' urlBootstrapCss+ addScript' urlBootstrapScript+ addStylesheet' urlSummernoteCss+ addScript' urlSummernoteScript)+ toWidget $ [julius|+$(document).ready(function(){+ var input = document.getElementById("#{rawJS theId}");+ $(input).summernote(#{rawJS cfg}).on('summernote.change',function(){+ $(input).text($(input).summernote('code'));+ });+});|]+ , fieldEnctype = UrlEncoded+ }+ where+ showVal = either id (pack . renderHtml)++-- | Summernote editor field with default settings.+snHtmlField :: YesodSummernote site => Field (HandlerT site IO) Html+snHtmlField = snHtmlFieldCustomized ""+++addScript' :: (MonadWidget m, HandlerSite m ~ site)+ => (site -> Either (Route site) Text)+ -> m ()+addScript' f = do+ y <- getYesod+ addScriptEither $ f y++addStylesheet' :: (MonadWidget m, HandlerSite m ~ site)+ => (site -> Either (Route site) Text)+ -> m ()+addStylesheet' f = do+ y <- getYesod+ addStylesheetEither $ f y
+ yesod-form-richtext.cabal view
@@ -0,0 +1,35 @@+name: yesod-form-richtext+version: 0.1.0.0+synopsis: Various rich-text WYSIWYG editors for Yesod forms.+description: Please see README.md+homepage: http://github.com/geraldus/yesod-form-richtext#readme+license: MIT+license-file: LICENSE+author: Arthur S. Fayzrakhmanov+maintainer: heraldhoi@gmail.com+copyright: 2016 Arthur S. Fayzrakhmanov+category: Web+build-type: Simple+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Yesod.Form.Summernote+ build-depends: base >= 4.7 && < 5+ , blaze-builder >= 0.2.1.4+ , blaze-html >= 0.5+ , shakespeare >= 2.0+ , text >= 0.9+ , xss-sanitize >= 0.3.0.1+ , yesod-core >= 1.4 && < 1.5+ , yesod-form >= 1.4.4.1 && < 1.5+ default-language: Haskell2010+ default-extensions: FlexibleContexts+ OverloadedStrings+ QuasiQuotes+ TypeFamilies+++source-repository head+ type: git+ location: https://github.com/geraldus/yesod-form-richtext