|
38 | 38 | import java.util.List; |
39 | 39 | import java.util.regex.Matcher; |
40 | 40 | import java.util.regex.Pattern; |
| 41 | +import javafx.application.Platform; |
| 42 | +import javafx.beans.property.BooleanProperty; |
| 43 | +import javafx.beans.property.SimpleBooleanProperty; |
41 | 44 | import javafx.scene.control.IndexRange; |
42 | 45 | import javafx.scene.input.KeyEvent; |
43 | 46 | import org.apache.commons.lang3.StringUtils; |
|
49 | 52 | import org.markdownwriterfx.options.Options; |
50 | 53 | import org.markdownwriterfx.util.Utils; |
51 | 54 | import com.vladsch.flexmark.ast.AutoLink; |
| 55 | +import com.vladsch.flexmark.ast.Block; |
| 56 | +import com.vladsch.flexmark.ast.BlockQuote; |
| 57 | +import com.vladsch.flexmark.ast.BulletListItem; |
52 | 58 | import com.vladsch.flexmark.ast.Code; |
| 59 | +import com.vladsch.flexmark.ast.ContentNode; |
53 | 60 | import com.vladsch.flexmark.ast.DelimitedNode; |
54 | 61 | import com.vladsch.flexmark.ast.Emphasis; |
| 62 | +import com.vladsch.flexmark.ast.FencedCodeBlock; |
55 | 63 | import com.vladsch.flexmark.ast.Heading; |
56 | 64 | import com.vladsch.flexmark.ast.Image; |
| 65 | +import com.vladsch.flexmark.ast.ImageRef; |
57 | 66 | import com.vladsch.flexmark.ast.Link; |
58 | 67 | import com.vladsch.flexmark.ast.LinkNode; |
| 68 | +import com.vladsch.flexmark.ast.LinkRef; |
59 | 69 | import com.vladsch.flexmark.ast.ListBlock; |
60 | 70 | import com.vladsch.flexmark.ast.ListItem; |
61 | 71 | import com.vladsch.flexmark.ast.MailLink; |
62 | 72 | import com.vladsch.flexmark.ast.Node; |
63 | 73 | import com.vladsch.flexmark.ast.NodeVisitor; |
| 74 | +import com.vladsch.flexmark.ast.OrderedListItem; |
| 75 | +import com.vladsch.flexmark.ast.Paragraph; |
64 | 76 | import com.vladsch.flexmark.ast.StrongEmphasis; |
65 | 77 | import com.vladsch.flexmark.ext.gfm.strikethrough.Strikethrough; |
66 | 78 | import com.vladsch.flexmark.util.sequence.BasedSequence; |
@@ -100,8 +112,100 @@ public class SmartEdit |
100 | 112 |
|
101 | 113 | // textArea.selectionProperty().addListener((ob, o, n) -> |
102 | 114 | // System.out.println(findNodes(n.getStart(), n.getEnd(), (s, e, node) -> true, true))); |
| 115 | + |
| 116 | + editor.markdownASTProperty().addListener((ob, o, n) -> updateStateProperties()); |
| 117 | + textArea.selectionProperty().addListener((ob, o, n) -> updateStateProperties()); |
103 | 118 | } |
104 | 119 |
|
| 120 | + //---- properties --------------------------------------------------------- |
| 121 | + |
| 122 | + private boolean updateStatePropertiesRunLaterPending; |
| 123 | + private void updateStateProperties() { |
| 124 | + // avoid too many (and useless) runLater() invocations |
| 125 | + if (updateStatePropertiesRunLaterPending) |
| 126 | + return; |
| 127 | + updateStatePropertiesRunLaterPending = true; |
| 128 | + |
| 129 | + Platform.runLater(() -> { |
| 130 | + updateStatePropertiesRunLaterPending = false; |
| 131 | + |
| 132 | + List<Node> nodesAtSelection = findNodesAtSelection((s, e, n) -> true, true); |
| 133 | + |
| 134 | + boolean bold = false; |
| 135 | + boolean italic = false; |
| 136 | + boolean code = false; |
| 137 | + boolean link = false; |
| 138 | + boolean image = false; |
| 139 | + boolean unorderedList = false; |
| 140 | + boolean orderedList = false; |
| 141 | + boolean blockquote = false; |
| 142 | + boolean fencedCode = false; |
| 143 | + boolean header = false; |
| 144 | + for (Node node : nodesAtSelection) { |
| 145 | + if (!bold && node instanceof StrongEmphasis) |
| 146 | + bold = true; |
| 147 | + else if (!italic && node instanceof Emphasis) |
| 148 | + italic = true; |
| 149 | + else if (!code && node instanceof Code) |
| 150 | + code = true; |
| 151 | + else if (!link && (node instanceof Link || node instanceof LinkRef)) |
| 152 | + link = true; |
| 153 | + else if (!image && (node instanceof Image || node instanceof ImageRef)) |
| 154 | + image = true; |
| 155 | + else if (!unorderedList && node instanceof BulletListItem) |
| 156 | + unorderedList = true; |
| 157 | + else if (!orderedList && node instanceof OrderedListItem) |
| 158 | + orderedList = true; |
| 159 | + else if (!blockquote && node instanceof BlockQuote) |
| 160 | + blockquote = true; |
| 161 | + else if (!fencedCode && node instanceof FencedCodeBlock) |
| 162 | + fencedCode = true; |
| 163 | + else if (!header && node instanceof Heading) |
| 164 | + header = true; |
| 165 | + } |
| 166 | + this.bold.set(bold); |
| 167 | + this.italic.set(italic); |
| 168 | + this.code.set(code); |
| 169 | + this.link.set(link); |
| 170 | + this.image.set(image); |
| 171 | + this.unorderedList.set(unorderedList); |
| 172 | + this.orderedList.set(orderedList); |
| 173 | + this.blockquote.set(blockquote); |
| 174 | + this.fencedCode.set(fencedCode); |
| 175 | + this.header.set(header); |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + private final BooleanProperty bold = new SimpleBooleanProperty(); |
| 180 | + public BooleanProperty boldProperty() { return bold; } |
| 181 | + |
| 182 | + private final BooleanProperty italic = new SimpleBooleanProperty(); |
| 183 | + public BooleanProperty italicProperty() { return italic; } |
| 184 | + |
| 185 | + private final BooleanProperty code = new SimpleBooleanProperty(); |
| 186 | + public BooleanProperty codeProperty() { return code; } |
| 187 | + |
| 188 | + private final BooleanProperty link = new SimpleBooleanProperty(); |
| 189 | + public BooleanProperty linkProperty() { return link; } |
| 190 | + |
| 191 | + private final BooleanProperty image = new SimpleBooleanProperty(); |
| 192 | + public BooleanProperty imageProperty() { return image; } |
| 193 | + |
| 194 | + private final BooleanProperty unorderedList = new SimpleBooleanProperty(); |
| 195 | + public BooleanProperty unorderedListProperty() { return unorderedList; } |
| 196 | + |
| 197 | + private final BooleanProperty orderedList = new SimpleBooleanProperty(); |
| 198 | + public BooleanProperty orderedListProperty() { return orderedList; } |
| 199 | + |
| 200 | + private final BooleanProperty blockquote = new SimpleBooleanProperty(); |
| 201 | + public BooleanProperty blockquoteProperty() { return blockquote; } |
| 202 | + |
| 203 | + private final BooleanProperty fencedCode = new SimpleBooleanProperty(); |
| 204 | + public BooleanProperty fencedCodeProperty() { return fencedCode; } |
| 205 | + |
| 206 | + private final BooleanProperty header = new SimpleBooleanProperty(); |
| 207 | + public BooleanProperty headerProperty() { return header; } |
| 208 | + |
105 | 209 | //---- enter ------------------------------------------------------------- |
106 | 210 |
|
107 | 211 | private void enterPressed(KeyEvent e) { |
|
0 commit comments