diff --git a/README.md b/README.md index 83e7141..22f894b 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,14 @@ -Markdown for Netbeans ![master](https://github.com/moacirrf/netbeans-markdown/actions/workflows/maven-publish.yml/badge.svg) -===================================== +# Markdown for Netbeans ![master](https://github.com/moacirrf/netbeans-markdown/actions/workflows/maven-publish.yml/badge.svg) + +*** ## Description -This plugin include some aditionals features to Apache Netbeans Markdown Editor. +This plugin include some additionals features to Apache Netbeans Markdown Editor. - Preview - Split Window -- Autocomplete +- Sugestion -### Problems -- A more accuracy syncronized scroll - +## Requirements - Apache Netbeans 14, 15 - JDK 11+ @@ -20,7 +19,9 @@ This plugin include some aditionals features to Apache Netbeans Markdown Editor. - Flexmark - BSD 2-Clause "Simplified" License - Apache Batik - Apache License, Version 2.0 - Jsoup - The MIT License + - Apache Commons Text - Apache License, Version 2.0 + -## Screenshots -![image](https://user-images.githubusercontent.com/950706/190041477-71d5b5fc-d887-4934-810a-0ceb1048c607.png) +## Screenshots + diff --git a/pom.xml b/pom.xml index 8a36116..5315f1f 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 io.github.moacirrf netbeans-markdown - 0.0.1 + 0.0.2 nbm Netbeans Markdown https://github.com/moacirrf/netbeans-markdown @@ -22,7 +22,7 @@ scm:git:https://github.com/moacirrf/netbeans-markdown scm:git:https://github.com/moacirrf/netbeans-markdown https://github.com/moacirrf/netbeans-markdown/tree/${project.scm.tag} - v0.0.1 + v0.0.2 @@ -160,6 +160,12 @@ 1.14 + + + org.apache.commons + commons-text + 1.9 + @@ -245,6 +251,21 @@ RELEASE140 jar + + + org.netbeans.api + org-netbeans-modules-editor-mimelookup + RELEASE140 + jar + + + + org.netbeans.api + org-netbeans-modules-editor-settings + RELEASE140 + jar + + UTF-8 diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/Icons.java b/src/main/java/io/github/moacirrf/netbeans/markdown/Icons.java index 73c46fb..1d4c62f 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/Icons.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/Icons.java @@ -24,7 +24,7 @@ */ public final class Icons { - private static final String PATH = "/io/github/moacirrf/netbeans/markdown/"; + private static final String PATH = "io/github/moacirrf/netbeans/markdown/"; public static ImageIcon getICON_PREVIEW() { return loadImageIcon(PATH + "icon_preview.png", true); diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/Installer.java b/src/main/java/io/github/moacirrf/netbeans/markdown/Installer.java index bf3ea7c..20ffb94 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/Installer.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/Installer.java @@ -23,7 +23,7 @@ * @author Moacir da Roza Flores */ public class Installer extends ModuleInstall { - + @Override public boolean closing() { TempDir.removeTempDir(); diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/MarkdownDataObject.java b/src/main/java/io/github/moacirrf/netbeans/markdown/MarkdownDataObject.java index 19dcf04..4af9a5d 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/MarkdownDataObject.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/MarkdownDataObject.java @@ -44,8 +44,7 @@ @DataObject.Registration( mimeType = MarkdownDataObject.MIME_TYPE, iconBase = "io/github/moacirrf/netbeans/markdown/markdown.png", - displayName = "#LBL_Markdown_LOADER", - position = 300 + displayName = "#LBL_Markdown_LOADER" ) @ActionReferences({ @ActionReference( diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/completion/AbstractCompletionItem.java b/src/main/java/io/github/moacirrf/netbeans/markdown/completion/AbstractCompletionItem.java index 323567c..e7c0c3c 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/completion/AbstractCompletionItem.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/completion/AbstractCompletionItem.java @@ -20,6 +20,7 @@ import java.awt.Font; import java.awt.Graphics; import java.awt.event.KeyEvent; +import java.util.Objects; import javax.swing.ImageIcon; import javax.swing.text.BadLocationException; import javax.swing.text.Document; @@ -40,7 +41,6 @@ public abstract class AbstractCompletionItem implements CompletionItem { private String leftHtmlText; private String righHtmlText; private String template; - private int startOffset; private int sortPriority; @@ -154,4 +154,30 @@ public void setSortPriority(int sortPriority) { this.sortPriority = sortPriority; } + @Override + public int hashCode() { + int hash = 7; + hash = 11 * hash + Objects.hashCode(this.leftHtmlText); + hash = 11 * hash + Objects.hashCode(this.righHtmlText); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final AbstractCompletionItem other = (AbstractCompletionItem) obj; + if (!Objects.equals(this.leftHtmlText, other.leftHtmlText)) { + return false; + } + return Objects.equals(this.righHtmlText, other.righHtmlText); + } + } diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/completion/CompletionItemImpl.java b/src/main/java/io/github/moacirrf/netbeans/markdown/completion/CompletionItemImpl.java index 6fcc9e3..0f7adb2 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/completion/CompletionItemImpl.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/completion/CompletionItemImpl.java @@ -30,6 +30,7 @@ public static AbstractCompletionItem newItem( String leftText, String rightText, int startOffset) { + var item = new CompletionItemImpl(); item.setIcon(icon); item.setLeftHtmlText(leftText); diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/completion/CompletionQuery.java b/src/main/java/io/github/moacirrf/netbeans/markdown/completion/CompletionQuery.java index b586431..93bf6be 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/completion/CompletionQuery.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/completion/CompletionQuery.java @@ -19,10 +19,10 @@ import static io.github.moacirrf.netbeans.markdown.completion.CompletionItemImpl.newItem; import static io.github.moacirrf.netbeans.markdown.Icons.getICON_COMPLETION; import java.io.IOException; -import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; +import java.util.Set; import javax.swing.text.Document; -import org.netbeans.spi.editor.completion.CompletionItem; import org.netbeans.spi.editor.completion.CompletionResultSet; import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery; import org.openide.util.Exceptions; @@ -33,21 +33,23 @@ */ public class CompletionQuery extends AsyncCompletionQuery { + private final Set itens = new HashSet<>(); + @Override protected void query(CompletionResultSet crs, Document document, int caretOffset) { - crs.addAllItems(getFormatHints(document, caretOffset)); - crs.addAllItems(getHeadings(document, caretOffset)); - crs.addAllItems(getBlockquotes(document, caretOffset)); - crs.addAllItems(getLists(document, caretOffset)); - crs.addAllItems(getCode(document, caretOffset)); - crs.addAllItems(getLinks(document, caretOffset)); - crs.addAllItems(getImages(document, caretOffset)); - crs.addAllItems(getTables(document, caretOffset)); + getFormatHints(caretOffset); + getHeadings(caretOffset); + getBlockquotes(caretOffset); + getLists(caretOffset); + getCode(caretOffset); + getLinks(caretOffset); + getImages(caretOffset); + getTables(caretOffset); + crs.addAllItems(itens); crs.finish(); } - private Collection getFormatHints(Document document, int caretOffset) { - var itens = new ArrayList(); + private Collection getFormatHints(int caretOffset) { var icon = getICON_COMPLETION(); itens.add(newItem(0, icon, "Italic 1", "*Italic 1*", caretOffset)); itens.add(newItem(0, icon, "Italic 2", "_Italic 2_", caretOffset)); @@ -61,8 +63,7 @@ private Collection getFormatHints(Document document, i return itens; } - private Collection getHeadings(Document document, int caretOffset) { - var itens = new ArrayList(); + private Collection getHeadings(int caretOffset) { var icon = getICON_COMPLETION(); itens.add(newItem(2, icon, "

Heading level 1

", "# Heading level 1", caretOffset)); itens.add(newItem(2, icon, "

Heading level 2

", "## Heading level 2", caretOffset)); @@ -74,16 +75,14 @@ private Collection getHeadings(Document document, int return itens; } - private Collection getBlockquotes(Document document, int caretOffset) { - var itens = new ArrayList(); + private Collection getBlockquotes(int caretOffset) { var icon = getICON_COMPLETION(); itens.add(newItem(3, icon, "Blockquote", "> Blockquote", caretOffset)); return itens; } - private Collection getLists(Document document, int caretOffset) { - var itens = new ArrayList(); + private Collection getLists(int caretOffset) { var icon = getICON_COMPLETION(); itens.add(newItem(4, icon, "Ordered List", "1. Item 1\n", caretOffset)); itens.add(newItem(4, icon, "Unordered List", "- Item 1\n", caretOffset)); @@ -91,16 +90,14 @@ private Collection getLists(Document document, int car return itens; } - private Collection getCode(Document document, int caretOffset) { - var itens = new ArrayList(); + private Collection getCode(int caretOffset) { var icon = getICON_COMPLETION(); itens.add(newItem(5, icon, "Code Block", " fun code(){}", caretOffset)); return itens; } - private Collection getLinks(Document document, int caretOffset) { - var itens = new ArrayList(); + private Collection getLinks(int caretOffset) { var icon = getICON_COMPLETION(); itens.add(newItem(6, icon, "Link", "[Description](https://netbeans.apache.org/).", caretOffset)); itens.add(newItem(6, icon, "Email URL", "\n" @@ -108,8 +105,7 @@ private Collection getLinks(Document document, int car return itens; } - private Collection getImages(Document document, int caretOffset) { - var itens = new ArrayList(); + private Collection getImages(int caretOffset) { var icon = getICON_COMPLETION(); itens.add(newItem(7, icon, "Image 1", "![Best IDE](https://netbeans.apache.org/images/nblogo48x48.png)", caretOffset)); @@ -118,11 +114,10 @@ private Collection getImages(Document document, int ca return itens; } - private Collection getTables(Document document, int caretOffset) { - var itens = new ArrayList(); + private Collection getTables(int caretOffset) { var icon = getICON_COMPLETION(); try ( var stream = getClass().getResourceAsStream("/io/github/moacirrf/netbeans/markdown/completion/table.md")) { - var item = newItem(7, icon, "Table", "A three Column table", caretOffset); + var item = newItem(7, icon, "Table", "A Three Column Table", caretOffset); item.setTemplate(new String(stream.readAllBytes())); itens.add(item); } catch (IOException ex) { diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/html/FirstElementAdjuster.java b/src/main/java/io/github/moacirrf/netbeans/markdown/html/FirstElementAdjuster.java index cd3f182..fa84abe 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/html/FirstElementAdjuster.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/html/FirstElementAdjuster.java @@ -25,8 +25,13 @@ public class FirstElementAdjuster implements HtmlAdjuster { @Override public Document adjust(Document inputDocument) { - inputDocument.getElementsByTag("body").first().firstElementChild() - .addClass("removeMarginPaddingTop"); + if (inputDocument != null) { + var body = inputDocument.getElementsByTag("body"); + if (body != null && body.first() != null && body.first().firstElementChild() != null) { + body.first().firstElementChild() + .addClass("removeMarginPaddingTop"); + } + } return inputDocument; } diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/html/HtmlBuilder.java b/src/main/java/io/github/moacirrf/netbeans/markdown/html/HtmlBuilder.java index d2a7629..c6f9440 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/html/HtmlBuilder.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/html/HtmlBuilder.java @@ -57,8 +57,13 @@ public String build(String markdownText) { var renderer = HtmlRenderer.builder(options).build(); Node document = parser.parse(markdownText); + if (document == null) { + return ""; + } Document doc = Jsoup.parse(renderer.render(document)); - + if (doc == null) { + return ""; + } if (!htmlAdjusters.isEmpty()) { for (var adjuster : htmlAdjusters) { doc = adjuster.adjust(doc); diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/html/LinksAdjuster.java b/src/main/java/io/github/moacirrf/netbeans/markdown/html/LinksAdjuster.java index 39c2785..0b41031 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/html/LinksAdjuster.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/html/LinksAdjuster.java @@ -29,7 +29,9 @@ public class LinksAdjuster implements HtmlAdjuster { @Override public Document adjust(Document inputDocument) { - fixLinksWithImage(inputDocument.getElementsByTag("a")); + if (inputDocument != null) { + fixLinksWithImage(inputDocument.getElementsByTag("a")); + } return inputDocument; } diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/html/TablesAdjuster.java b/src/main/java/io/github/moacirrf/netbeans/markdown/html/TablesAdjuster.java index 2db7241..337894f 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/html/TablesAdjuster.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/html/TablesAdjuster.java @@ -29,10 +29,11 @@ public class TablesAdjuster implements HtmlAdjuster { @Override public Document adjust(Document document) { - - document.getElementsByTag("table").forEach(table - -> fixTableRowsStyle(table.getElementsByTag("tr")) - ); + if (document != null && document.getElementsByTag("table") != null) { + document.getElementsByTag("table").forEach(table + -> fixTableRowsStyle(table.getElementsByTag("tr")) + ); + } return document; } diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/MultiViewSplitEditorElement.java b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/MultiViewSplitEditorElement.java index 3956381..662c778 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/MultiViewSplitEditorElement.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/MultiViewSplitEditorElement.java @@ -79,7 +79,6 @@ public void fileChanged(FileEvent fe) { super.fileChanged(fe); syncronizeScrolls(); } - }); rightJScrollPane.setFileObject(mdFile); @@ -106,17 +105,17 @@ private void syncronizeScrolls() { } private void setScrollToMaximum(JScrollPane scrollPane) { - Point p = scrollPane.getViewport().getViewPosition(); - p.y = Integer.MAX_VALUE; - scrollPane.getViewport().setViewPosition(p); + var viewPort = scrollPane.getViewport(); + var viewPosition = viewPort.getViewPosition(); + var viewSize = viewPort.getViewSize(); + + viewPosition.y = viewSize.height; + scrollPane.getViewport().setViewPosition(viewPosition); } private boolean isScrolledToMaximum(JScrollPane scrollPane) { var viewPort = scrollPane.getViewport(); - if ((viewPort.getViewSize().height - viewPort.getExtentSize().getHeight()) == viewPort.getViewPosition().y) { - return true; - } - return false; + return (viewPort.getViewSize().height - viewPort.getExtentSize().getHeight()) == viewPort.getViewPosition().y; } diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/TopBar.java b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/TopBar.java index 2e16d49..ebf9e4a 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/TopBar.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/TopBar.java @@ -20,6 +20,7 @@ import java.awt.Graphics; import static javax.swing.JSplitPane.HORIZONTAL_SPLIT; import static javax.swing.JSplitPane.VERTICAL_SPLIT; +import javax.swing.SwingUtilities; public class TopBar extends javax.swing.JPanel { @@ -125,34 +126,40 @@ private void showPreviewBtnActionPerformed(java.awt.event.ActionEvent evt) {//GE }//GEN-LAST:event_showPreviewBtnActionPerformed private void onlySource() { - splitPanel.getSplitPanel().getLeftComponent().setVisible(true); - splitPanel.getSplitPanel().getRightComponent().setVisible(false); - splitModeOn = false; + SwingUtilities.invokeLater(() -> { + splitPanel.getSplitPanel().getLeftComponent().setVisible(true); + splitPanel.getSplitPanel().getRightComponent().setVisible(false); + splitModeOn = false; + }); } private void split() { - splitModeBtn.setSelected(true); - splitPanel.getSplitPanel().getLeftComponent().setVisible(true); - splitPanel.getSplitPanel().getRightComponent().setVisible(true); - if (splitModeOn) { - switch (splitPanel.getSplitPanel().getOrientation()) { - case HORIZONTAL_SPLIT: - splitPanel.getSplitPanel().setOrientation(VERTICAL_SPLIT); - this.splitModeBtn.setIcon(Icons.getICON_VERTICAL_SPLIT()); - break; - default: - splitPanel.getSplitPanel().setOrientation(HORIZONTAL_SPLIT); - this.splitModeBtn.setIcon(Icons.getICON_HORIZONTAL_SPLIT()); + SwingUtilities.invokeLater(() -> { + splitModeBtn.setSelected(true); + splitPanel.getSplitPanel().getLeftComponent().setVisible(true); + splitPanel.getSplitPanel().getRightComponent().setVisible(true); + if (splitModeOn) { + switch (splitPanel.getSplitPanel().getOrientation()) { + case HORIZONTAL_SPLIT: + splitPanel.getSplitPanel().setOrientation(VERTICAL_SPLIT); + this.splitModeBtn.setIcon(Icons.getICON_VERTICAL_SPLIT()); + break; + default: + splitPanel.getSplitPanel().setOrientation(HORIZONTAL_SPLIT); + this.splitModeBtn.setIcon(Icons.getICON_HORIZONTAL_SPLIT()); + } } - } - splitModeOn = true; - refreshDividerLocation = true; + splitModeOn = true; + refreshDividerLocation = true; + }); } private void onlyPreview() { - splitPanel.getSplitPanel().getLeftComponent().setVisible(false); - splitPanel.getSplitPanel().getRightComponent().setVisible(true); - splitModeOn = false; + SwingUtilities.invokeLater(() -> { + splitPanel.getSplitPanel().getLeftComponent().setVisible(false); + splitPanel.getSplitPanel().getRightComponent().setVisible(true); + splitModeOn = false; + }); } @Override diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/LocalImageView.java b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/LocalImageView.java index a86c847..232928c 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/LocalImageView.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/LocalImageView.java @@ -32,6 +32,7 @@ import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.image.PNGTranscoder; import org.openide.util.Exceptions; +import org.openide.util.ImageUtilities; /** * SVG images need to be converted to PNG, this class @@ -70,7 +71,7 @@ public URL convertToPNG(URL url) { return url; } - Path imageTemp = Paths.get(tempDir.toString(), new Date().getTime() + "_" + fileName + ".png"); + Path imageTemp = Paths.get(tempDir.toString(), fileName + ".png"); imageTemp.toFile().setWritable(true); PNGTranscoder t = new PNGTranscoder(); TranscoderInput input = new TranscoderInput(url.toString()); diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/MarkdownPreviewScrollPane.java b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/MarkdownPreviewScrollPane.java index a8bb714..8de4e20 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/MarkdownPreviewScrollPane.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/MarkdownPreviewScrollPane.java @@ -36,7 +36,7 @@ public class MarkdownPreviewScrollPane extends JScrollPane { public MarkdownPreviewScrollPane() { this.initComponents(); setAutoscrolls(false); - this.setBorder(BorderFactory.createEmptyBorder(22, 0, 0, 0)); + this.setBorder(BorderFactory.createEmptyBorder(22, 0, 9, 0)); editorPane.addHyperlinkListener((HyperlinkEvent e) -> { if (e.getInputEvent() instanceof MouseEvent) { if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) { @@ -48,7 +48,6 @@ public MarkdownPreviewScrollPane() { private void initComponents() { editorPane = new JEditorPane(); - editorPane.setSize(super.getSize()); editorPane.setEditable(false); editorPane.setEditorKit(new MarkdownEditorKit()); setViewportView(editorPane); @@ -63,9 +62,8 @@ public void setFileObject(FileObject fileObject) { public void fillEditorPane() { try { - String html = HtmlBuilder.getInstance() - .build(fileObject.asText()); - editorPane.setText(html); + editorPane.setText(HtmlBuilder.getInstance() + .build(fileObject.asText())); } catch (IOException ex) { Exceptions.printStackTrace(ex); } diff --git a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/ThemeResolver.java b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/ThemeResolver.java index 606bfb5..b61a4e2 100644 --- a/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/ThemeResolver.java +++ b/src/main/java/io/github/moacirrf/netbeans/markdown/ui/preview/ThemeResolver.java @@ -16,29 +16,91 @@ */ package io.github.moacirrf.netbeans.markdown.ui.preview; +import java.awt.Color; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import javax.swing.UIManager; +import javax.swing.text.StyleConstants; import javax.swing.text.html.StyleSheet; +import org.apache.commons.text.StringSubstitutor; +import org.netbeans.api.editor.mimelookup.MimeLookup; +import org.netbeans.api.editor.mimelookup.MimePath; +import org.netbeans.api.editor.settings.FontColorNames; +import org.netbeans.api.editor.settings.FontColorSettings; +import org.openide.util.Exceptions; public final class ThemeResolver { - public static final String DEFAULT_CSS = "common.css"; - public static final String LIGHT_CSS = "light.css"; - public static final String DARK_CSS = "dark.css"; - public static final String GENERIC_CSS = "generic.css"; + public static final String STYLE_CSS = "/io/github/moacirrf/netbeans/markdown/ui/preview/style.css.template"; + + private FontColorSettings fontColorSettings; public StyleSheet resolve() { + fontColorSettings = (FontColorSettings) MimeLookup.getLookup(MimePath.parse("text/x-java")) + .lookup(FontColorSettings.class); StyleSheet styleSheet = new StyleSheet(); - styleSheet.importStyleSheet(this.getClass().getResource(DEFAULT_CSS)); - String theme = UIManager.getLookAndFeel().getName().toLowerCase(); - - if (theme.contains("dark")) { - styleSheet.importStyleSheet(this.getClass().getResource(DARK_CSS)); - } else if (theme.contains("flatlaf light")) { - styleSheet.importStyleSheet(this.getClass().getResource(LIGHT_CSS)); - } else { - styleSheet.importStyleSheet(this.getClass().getResource(GENERIC_CSS)); + try { + String style = new String(getClass().getResourceAsStream(STYLE_CSS).readAllBytes()); + if (UIManager.getBoolean("nb.dark.theme")) { + style = StringSubstitutor.replace(style, getDarkStyle()); + styleSheet.addRule(style); + } else { + style = StringSubstitutor.replace(style, getLightStyle()); + styleSheet.addRule(style); + } + return styleSheet; + } catch (IOException ex) { + Exceptions.printStackTrace(ex); } - return styleSheet; } + + private Map getLightStyle() { + HashMap maps = new HashMap<>(); + maps.put("blockquote.bgColor", "#F5F7F9"); + maps.put("blockquote.borderLeftColor", "#a1c1dd"); + maps.put("removeColorLinkWithImage.color", "#FFFFFF"); + maps.put("pre.bgColor", "#F5F7F9"); + maps.put("code.bgColor", "#F5F7F9"); + maps.put("tdTh.borderColor", "#515151"); + setFontFamilySize(maps); + return maps; + } + + private Map getDarkStyle() { + HashMap maps = new HashMap<>(); + maps.put("blockquote.bgColor", "#3C3D3E"); + maps.put("blockquote.borderLeftColor", "#476387"); + maps.put("removeColorLinkWithImage.color", "#2B2B2B"); + maps.put("pre.bgColor", "#3C3D3E"); + maps.put("code.bgColor", "#3C3D3E"); + maps.put("tdTh.borderColor", "#515151"); + setFontFamilySize(maps); + return maps; + } + + /** + * See configuration of fontSize, fontFamily, fontColor, background from + * Preferences\Font &Colors + * + * + * @param maps + */ + private void setFontFamilySize(HashMap maps) { + + var att = fontColorSettings.getFontColors(FontColorNames.DEFAULT_COLORING); + + maps.put("fontFamily", att.getAttribute(StyleConstants.FontConstants.FontFamily)); + maps.put("fontSize", att.getAttribute(StyleConstants.FontConstants.FontSize)); + + Color fontColor = (Color) att.getAttribute(StyleConstants.FontConstants.Foreground); + Color bgColor = (Color) att.getAttribute(StyleConstants.FontConstants.Background); + maps.put("body.color", toRGB(fontColor)); + maps.put("body.bgColor", toRGB(bgColor)); + } + + private String toRGB(Color color) { + return String.format("rgb(%s,%s,%s)", color.getRed(), color.getGreen(), color.getBlue()); + } } diff --git a/src/main/resources/io/github/moacirrf/netbeans/markdown/Bundle.properties b/src/main/resources/io/github/moacirrf/netbeans/markdown/Bundle.properties index 84010bf..62fd70a 100644 --- a/src/main/resources/io/github/moacirrf/netbeans/markdown/Bundle.properties +++ b/src/main/resources/io/github/moacirrf/netbeans/markdown/Bundle.properties @@ -2,5 +2,5 @@ OpenIDE-Module-Name= Netbeans Markdown OpenIDE-Module-Short-Description=Markdown support for Apache Netbeans. OpenIDE-Module-Long-Description=Markdown support for Apache Netbeans -OpenIDE-Module-Display-Category=Tools +OpenIDE-Module-Display-Category=Editing #Fri Sep 02 13:26:18 BRT 2022 diff --git a/src/main/resources/io/github/moacirrf/netbeans/markdown/MarkdownTemplate.md b/src/main/resources/io/github/moacirrf/netbeans/markdown/MarkdownTemplate.md index 71889d5..91caca0 100644 --- a/src/main/resources/io/github/moacirrf/netbeans/markdown/MarkdownTemplate.md +++ b/src/main/resources/io/github/moacirrf/netbeans/markdown/MarkdownTemplate.md @@ -1,4 +1,6 @@ # The largest heading + ## The second largest heading + --- > That is a quote \ No newline at end of file diff --git a/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/dark.css b/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/dark.css deleted file mode 100644 index b921509..0000000 --- a/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/dark.css +++ /dev/null @@ -1,25 +0,0 @@ -body { - color: #a8b6c4; - background-color: #2B2B2B; -} - -.removeColorLinkWithImage { - color: #2B2B2B; -} - -pre { - background-color: #3C3D3E; -} - -blockquote { - background-color: #3C3D3E; - border-left-color: #476387; -} - -code { - background-color: #3C3D3E; -} - -td, th{ - border-color: #515151; -} \ No newline at end of file diff --git a/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/generic.css b/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/generic.css deleted file mode 100644 index dff0a45..0000000 --- a/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/generic.css +++ /dev/null @@ -1,25 +0,0 @@ -body { - color: #000000; - background-color: #FFFFFF; -} - -.removeColorLinkWithImage { - color: #FFFFFF; -} - -pre { - background-color: #F5F7F9; -} - -blockquote { - background-color: #F5F7F9; - border-left-color: #666666; -} - -code { - background-color: #F5F7F9; -} - -td, th{ - border-color: #000000; -} \ No newline at end of file diff --git a/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/light.css b/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/light.css deleted file mode 100644 index b1f2037..0000000 --- a/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/light.css +++ /dev/null @@ -1,25 +0,0 @@ -body { - color: #a8b6c4; - background-color: #FFFFFF; -} - -.removeColorLinkWithImage { - color: #FFFFFF; -} - -pre { - background-color: #F5F7F9; -} - -blockquote { - background-color: #F5F7F9; - border-left-color: #a1c1dd; -} - -code { - background-color: #F5F7F9; -} - -td, th{ - border-color: #d1d1d1; -} \ No newline at end of file diff --git a/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/common.css b/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/style.css.template similarity index 82% rename from src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/common.css rename to src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/style.css.template index d9ca63c..99a167f 100644 --- a/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/common.css +++ b/src/main/resources/io/github/moacirrf/netbeans/markdown/ui/preview/style.css.template @@ -4,15 +4,16 @@ body { padding-right: 15px; margin-left: 0; margin-right: 0; + color: ${body.color}; + background-color: ${body.bgColor}; } -.removeMarginPaddingTop{ - padding-top: 0px; - margin-top: 0px; +p, h1, h2, h3, h4, h5, h6, a, code, table, li { + font-family: ${fontFamily}; } -p, h1, h2, h3, h4, h5, h6, a, code, table { - font-family: Noto Sans, sans-serif; +p, a, code, table, li { + font-size: ${fontSize}; } p { @@ -76,6 +77,8 @@ blockquote { border-left-width: 3px; padding-left: 20px; margin-bottom: 10px; + background-color: ${blockquote.bgColor}; + border-left-color: ${blockquote.borderLeftColor}; } blockquote p { @@ -127,7 +130,6 @@ ol ul{ li{ margin: 3px; - font-family: Noto Sans, sans-serif; } li p{ @@ -152,6 +154,7 @@ pre { padding-bottom:10px; padding-left: 20px; padding-right: 20px; + background-color: ${pre.bgColor}; } a { @@ -168,11 +171,17 @@ td, th{ border-style: solid; border-width: 1px; padding: 10px; + border-color: ${tdTh.borderColor}; } + th { font-weight: bold; } +code { + background-color: ${code.bgColor}; +} + .noBorderTop{ border-top-width: 0px; } @@ -186,4 +195,11 @@ th { border-left-width: 0px; } +.removeColorLinkWithImage { + color: ${removeColorLinkWithImage.color}; +} +.removeMarginPaddingTop{ + padding-top: 0px; + margin-top: 0px; +}