You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarkdownTextArea.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.panels;
  17. import org.apache.wicket.ajax.AbstractAjaxTimerBehavior;
  18. import org.apache.wicket.ajax.AjaxRequestTarget;
  19. import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
  20. import org.apache.wicket.markup.html.basic.Label;
  21. import org.apache.wicket.markup.html.form.TextArea;
  22. import org.apache.wicket.model.IModel;
  23. import org.apache.wicket.model.PropertyModel;
  24. import org.apache.wicket.util.time.Duration;
  25. import com.gitblit.utils.MarkdownUtils;
  26. import com.gitblit.wicket.GitBlitWebApp;
  27. public class MarkdownTextArea extends TextArea {
  28. private static final long serialVersionUID = 1L;
  29. protected String repositoryName;
  30. protected String text = "";
  31. public MarkdownTextArea(String id, final IModel<String> previewModel, final Label previewLabel) {
  32. super(id);
  33. this.repositoryName = repositoryName;
  34. setModel(new PropertyModel(this, "text"));
  35. add(new AjaxFormComponentUpdatingBehavior("onblur") {
  36. private static final long serialVersionUID = 1L;
  37. @Override
  38. protected void onUpdate(AjaxRequestTarget target) {
  39. renderPreview(previewModel);
  40. if (target != null) {
  41. target.addComponent(previewLabel);
  42. }
  43. }
  44. });
  45. add(new AjaxFormComponentUpdatingBehavior("onchange") {
  46. private static final long serialVersionUID = 1L;
  47. @Override
  48. protected void onUpdate(AjaxRequestTarget target) {
  49. renderPreview(previewModel);
  50. if (target != null) {
  51. target.addComponent(previewLabel);
  52. }
  53. }
  54. });
  55. add(new KeepAliveBehavior());
  56. setOutputMarkupId(true);
  57. }
  58. protected void renderPreview(IModel<String> previewModel) {
  59. if (text == null) {
  60. return;
  61. }
  62. String html = MarkdownUtils.transformGFM(GitBlitWebApp.get().settings(), text, repositoryName);
  63. previewModel.setObject(html);
  64. }
  65. public String getText() {
  66. return text;
  67. }
  68. public void setText(String text) {
  69. this.text = text;
  70. }
  71. public void setRepository(String repositoryName) {
  72. this.repositoryName = repositoryName;
  73. }
  74. // @Override
  75. // protected void onBeforeRender() {
  76. // super.onBeforeRender();
  77. // add(new RichTextSetActiveTextFieldAttributeModifier(this.getMarkupId()));
  78. // }
  79. //
  80. // private class RichTextSetActiveTextFieldAttributeModifier extends AttributeModifier {
  81. //
  82. // private static final long serialVersionUID = 1L;
  83. //
  84. // public RichTextSetActiveTextFieldAttributeModifier(String markupId) {
  85. // super("onClick", true, new Model("richTextSetActiveTextField('" + markupId + "');"));
  86. // }
  87. // }
  88. private class KeepAliveBehavior extends AbstractAjaxTimerBehavior {
  89. private static final long serialVersionUID = 1L;
  90. public KeepAliveBehavior() {
  91. super(Duration.minutes(5));
  92. }
  93. @Override
  94. protected void onTimer(AjaxRequestTarget target) {
  95. // prevent wicket changing focus
  96. target.focusComponent(null);
  97. }
  98. }
  99. }