View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.markdowneditor;
23  
24  import java.io.IOException;
25  import java.util.Map;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.context.ResponseWriter;
30  import javax.faces.convert.Converter;
31  
32  import org.primefaces.component.inputtextarea.InputTextarea;
33  import org.primefaces.renderkit.InputRenderer;
34  import org.primefaces.util.ComponentUtils;
35  import org.primefaces.util.HTML;
36  import org.primefaces.util.WidgetBuilder;
37  
38  /**
39   * Markdown Editor.
40   *
41   * @since 14.0.0
42   */
43  public class MarkdownEditorRenderer extends InputRenderer {
44  
45      @Override
46      public void decode(FacesContext context, UIComponent component) {
47          MarkdownEditor editor = (MarkdownEditor) component;
48  
49          if (!shouldDecode(editor)) {
50              return;
51          }
52  
53          // set value
54          String clientId = editor.getClientId(context);
55          Map<String, String> params = context.getExternalContext().getRequestParameterMap();
56          if (params.containsKey(clientId)) {
57              String value = editor.sanitizeHtml(context, params.get(clientId));
58              editor.setSubmittedValue(value);
59          }
60  
61          // decode behaviors
62          decodeBehaviors(context, editor);
63      }
64  
65      @Override
66      public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
67          MarkdownEditor editor = (MarkdownEditor) component;
68          editor.checkSecurity(context);
69          encodeMarkup(context, editor);
70          encodeScript(context, editor);
71      }
72  
73      protected void encodeScript(FacesContext context, MarkdownEditor editor) throws IOException {
74          WidgetBuilder wb = getWidgetBuilder(context);
75          wb.init("ExtMarkdownEditor", editor)
76                      .attr("minHeight", editor.getMinHeight(), "300px")
77                      .attr("maxHeight", editor.getMaxHeight(), null)
78                      .attr("mode", editor.getMode())
79                      .attr("toolbar", editor.getToolbar())
80                      .attr("placeholder", editor.getPlaceholder())
81                      .attr("direction", ComponentUtils.isRTL(context, editor) ? "rtl" : "ltr", "ltr")
82                      .attr("sideBySideFullscreen", editor.getSideBySideFullscreen(), true)
83                      .attr("indentWithTabs", editor.getIndentWithTabs(), true)
84                      .attr("lineNumbers", editor.getLineNumbers(), false)
85                      .attr("promptURLs", editor.getPromptURLs(), false)
86                      .attr("tabSize", editor.getTabSize(), 2)
87                      .nativeAttr("extender", editor.getExtender());
88  
89          encodeClientBehaviors(context, editor);
90  
91          wb.finish();
92      }
93  
94      protected void encodeMarkup(FacesContext context, MarkdownEditor editor) throws IOException {
95          ResponseWriter writer = context.getResponseWriter();
96          String clientId = editor.getClientId(context);
97  
98          writer.startElement("textarea", null);
99          writer.writeAttribute("id", clientId, null);
100         writer.writeAttribute("name", clientId, null);
101         writer.writeAttribute("class", createStyleClass(editor, InputTextarea.STYLE_CLASS), "styleClass");
102         writer.writeAttribute("style", "display:none", "style");
103 
104         renderAccessibilityAttributes(context, editor);
105         renderRTLDirection(context, editor);
106         renderPassThruAttributes(context, editor, HTML.TEXTAREA_ATTRS_WITHOUT_EVENTS);
107         renderDomEvents(context, editor, HTML.INPUT_TEXT_EVENTS);
108         renderValidationMetadata(context, editor);
109 
110         String valueToRender = ComponentUtils.getValueToRender(context, editor);
111         if (valueToRender != null) {
112             writer.writeText(valueToRender, "value");
113         }
114 
115         writer.endElement("textarea");
116     }
117 
118     @Override
119     public Object getConvertedValue(final FacesContext context, final UIComponent component,
120                 final Object submittedValue) {
121         final MarkdownEditor editor = (MarkdownEditor) component;
122         final String value = (String) submittedValue;
123         final Converter converter = ComponentUtils.getConverter(context, component);
124 
125         if (converter != null) {
126             return converter.getAsObject(context, editor, value);
127         }
128 
129         return value;
130     }
131 }