Skip to content

Commit

Permalink
Merge pull request #6401 from moacirrf/ft-markdown-checkbox
Browse files Browse the repository at this point in the history
Checkbox support on Markdown preview
  • Loading branch information
mbien committed Sep 22, 2023
2 parents e0842b7 + 5d92f5d commit 9afb06a
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 11 deletions.
1 change: 1 addition & 0 deletions ide/libs.flexmark/external/binaries-list
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ AE5AFA76A669E06B3A65255C2ED775EDE5CD3EB1 com.vladsch.flexmark:flexmark-util-depe
18133DD81887C512E2F56FEB3C14A8A1210F30EA com.vladsch.flexmark:flexmark-util-misc:0.62.2
87AF2803A63B5B07CC42D9C1D98F8ECD4F83FBCC com.vladsch.flexmark:flexmark-util-sequence:0.62.2
CA49BD94860A5CCEDD82BDE96ECE831F16C66FF1 com.vladsch.flexmark:flexmark-util-visitor:0.62.2
1C0FD7A70AFAC3A93459E49089E2D1B2BF72E590 com.vladsch.flexmark:flexmark-ext-gfm-tasklist:0.62.2
36DA09A8F68484523FA2AAA100399D612B247D67 org.jsoup:jsoup:1.11.3
2 changes: 1 addition & 1 deletion ide/libs.flexmark/external/flexmark-0.62.2-license.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Version: 0.62.2
License: BSD-flexmark
Description: FlexMark library
Origin: https://github.com/vsch/flexmark-java
Files: flexmark-0.62.2.jar flexmark-ext-anchorlink-0.62.2.jar flexmark-ext-emoji-0.62.2.jar flexmark-ext-tables-0.62.2.jar flexmark-html2md-converter-0.62.2.jar flexmark-util-ast-0.62.2.jar flexmark-util-builder-0.62.2.jar flexmark-util-collection-0.62.2.jar flexmark-util-data-0.62.2.jar flexmark-util-dependency-0.62.2.jar flexmark-util-format-0.62.2.jar flexmark-util-html-0.62.2.jar flexmark-util-misc-0.62.2.jar flexmark-util-sequence-0.62.2.jar flexmark-util-visitor-0.62.2.jar
Files: flexmark-0.62.2.jar flexmark-ext-anchorlink-0.62.2.jar flexmark-ext-emoji-0.62.2.jar flexmark-ext-tables-0.62.2.jar flexmark-html2md-converter-0.62.2.jar flexmark-util-ast-0.62.2.jar flexmark-util-builder-0.62.2.jar flexmark-util-collection-0.62.2.jar flexmark-util-data-0.62.2.jar flexmark-util-dependency-0.62.2.jar flexmark-util-format-0.62.2.jar flexmark-util-html-0.62.2.jar flexmark-util-misc-0.62.2.jar flexmark-util-sequence-0.62.2.jar flexmark-util-visitor-0.62.2.jar flexmark-ext-gfm-tasklist-0.62.2.jar

Copyright (c) 2015-2016, Atlassian Pty Ltd
All rights reserved.
Expand Down
1 change: 1 addition & 0 deletions ide/libs.flexmark/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ release.external/flexmark-util-visitor-0.62.2.jar=modules/ext/flexmark-util-visi
release.external/flexmark-ext-emoji-0.62.2.jar=modules/ext/flexmark-ext-emoji-0.62.2.jar
release.external/flexmark-ext-anchorlink-0.62.2.jar=modules/ext/flexmark-ext-anchorlink-0.62.2.jar
release.external/flexmark-ext-tables-0.62.2.jar=modules/ext/flexmark-ext-tables-0.62.2.jar
release.external/flexmark-ext-gfm-tasklist-0.62.2.jar=modules/ext/flexmark-ext-gfm-tasklist-0.62.2.jar
release.external/jsoup-1.11.3.jar=modules/ext/jsoup-1.11.3.jar
4 changes: 4 additions & 0 deletions ide/libs.flexmark/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
<runtime-relative-path>ext/flexmark-0.62.2.jar</runtime-relative-path>
<binary-origin>external/flexmark-0.62.2.jar</binary-origin>
</class-path-extension>
<class-path-extension>
<runtime-relative-path>ext/flexmark-ext-gfm-tasklist-0.62.2.jar</runtime-relative-path>
<binary-origin>external/flexmark-ext-gfm-tasklist-0.62.2.jar</binary-origin>
</class-path-extension>
</data>
</configuration>
</project>
2 changes: 1 addition & 1 deletion ide/markdown/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>1.14</specification-version>
<specification-version>1.15</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.netbeans.modules.markdown;

import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension;
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
Expand Down Expand Up @@ -48,6 +49,7 @@
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.netbeans.modules.markdown.ui.preview.MarkdownEditorKit;
import org.openide.awt.HtmlBrowser;
import org.openide.awt.UndoRedo;
import org.openide.cookies.EditorCookie;
Expand Down Expand Up @@ -91,19 +93,17 @@ public class MarkdownViewerElement implements MultiViewElement {
static final DataHolder OPTIONS = new MutableDataSet()
.set(Parser.EXTENSIONS, Arrays.asList(
AnchorLinkExtension.create(),
TablesExtension.create()
TablesExtension.create(),
TaskListExtension.create()
))

.set(HtmlRenderer.INDENT_SIZE, 2)
.set(HtmlRenderer.RENDER_HEADER_ID, true)
.set(HtmlRenderer.FENCED_CODE_LANGUAGE_CLASS_PREFIX, "")

// JEditorPane search for the name attribute
.set(AnchorLinkExtension.ANCHORLINKS_SET_NAME, true)
.set(AnchorLinkExtension.ANCHORLINKS_SET_ID, false)
.set(AnchorLinkExtension.ANCHORLINKS_ANCHOR_CLASS, "")
.set(AnchorLinkExtension.ANCHORLINKS_TEXT_PREFIX, "")

// Make the table generation SWING Friendly
.set(TablesExtension.COLUMN_SPANS, false)
.set(TablesExtension.MIN_HEADER_ROWS, 1)
Expand Down Expand Up @@ -144,10 +144,10 @@ public MarkdownViewerElement(Lookup lookup) {
public JComponent getVisualRepresentation() {
if (component == null) {
viewer = new JEditorPane();
viewer.setContentType("text/html");
viewer.setEditorKit(new MarkdownEditorKit());
viewer.setEditable(false);
viewer.addHyperlinkListener(this::linkHandler);

JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(viewer), BorderLayout.CENTER);

Expand Down Expand Up @@ -230,7 +230,6 @@ public CloseOperationState canCloseElement() {
return CloseOperationState.STATE_OK;
}


@Messages("TXT_MarkdownViewerElement_Error=<html>Something happened during markdown parsing.")
public void updateView() {
if ((source != null) && (viewer != null)) {
Expand All @@ -241,14 +240,14 @@ public void updateView() {
String html = renderer.render(parser.parse(content));

Reader htmlReader = new StringReader(html);
HTMLEditorKit kit = new HTMLEditorKit();
HTMLEditorKit kit = (HTMLEditorKit) viewer.getEditorKit();

HTMLDocument doc = (HTMLDocument) viewer.getDocument();

// Would be better to create some diff and update the changed elemets
doc.remove(0, doc.getLength());
kit.read(htmlReader, doc, 0);

viewer.scrollRectToVisible(vis);

} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.markdown.ui.preview;

import org.netbeans.modules.markdown.ui.preview.views.MarkdownViewFactory;
import javax.swing.text.ViewFactory;
import javax.swing.text.html.HTMLEditorKit;

/**
*
* @author moacirrf
*/
public class MarkdownEditorKit extends HTMLEditorKit {

private final transient ViewFactory viewFactory;

public MarkdownEditorKit() {
super();
this.viewFactory = new MarkdownViewFactory();
}

@Override
public ViewFactory getViewFactory() {
return viewFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.markdown.ui.preview.views;

import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.text.Element;
import javax.swing.text.html.FormView;

/**
*
* @author moacirrf
*/
public class CheckboxView extends FormView {

public CheckboxView(Element elem) {
super(elem);
}

@Override
protected Component createComponent() {
Component component = super.createComponent();
if (component instanceof JCheckBox) {
JCheckBox c = (JCheckBox) component;
c.setBorder(BorderFactory.createEmptyBorder(0, 0, -4, 0));
c.addActionListener((ActionEvent e) -> c.setSelected(!c.isSelected()));
}
return component;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.markdown.ui.preview.views;

import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;
import javax.swing.text.View;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;

/**
* This class creates Views for a JEditorPane, it receives and HTML element and
* you must create the most similar swing component to this html element. For
* example a HTML input type checkbox, its a JCheckBox, and image can be a
* JLabel and so on.
*
* @author moacirrf
*/
public class MarkdownViewFactory extends HTMLEditorKit.HTMLFactory {

@Override
public View create(Element elem) {

if (isElementOfTag(elem, HTML.Tag.INPUT)) {
return new CheckboxView(elem);
}

return super.create(elem);
}

public boolean isElementOfTag(Element elem, HTML.Tag tag) {

AttributeSet attrs = elem.getAttributes();
Object elementName
= attrs.getAttribute(AbstractDocument.ElementNameAttribute);
Object o = (elementName != null)
? null : attrs.getAttribute(StyleConstants.NameAttribute);
HTML.Tag kind = (HTML.Tag) o;

return (o instanceof HTML.Tag) && (kind == tag);

}
}

0 comments on commit 9afb06a

Please sign in to comment.