Skip to content

Commit

Permalink
blockquote support and massivelly improve spacing/padding handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Jan 4, 2024
1 parent 86d9e84 commit b1d2fc1
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 57 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "dev.dediamondpro"
version = "1.0-SNAPSHOT62"
version = "1.0-SNAPSHOT70"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LayoutConfigImpl(
strikethrough: Boolean = false,
partOfLink: Boolean = false,
preFormatted: Boolean = false,
spacingConfig: SpacingConfig = SpacingConfig(4f, 4f, 4f, 16f),
spacingConfig: SpacingConfig = SpacingConfig(2f, 6f, 16f),
headingConfig: HeadingConfig = HeadingConfig(2f, 1.66f, 1.33f, 1f, 1.2f, 1f),
imageProvider: ImageProvider = DefaultImageProvider.INSTANCE,
browserProvider: BrowserProvider = ElementaBrowserProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class MineMarkComponent(
.addElement(Elements.IMAGE, ::MarkdownImageComponent)
.addElement(Elements.HORIZONTAL_LINE, ::MarkdownHorizontalLineComponent)
.addElement(Elements.LIST_ELEMENT, ::MarkdownListElementComponent)
.addElement(Elements.BLOCKQUOTE, ::MarkdownBlockquoteComponent)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.LayoutData
import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elements.Element
import dev.dediamondpro.minemark.elements.impl.BlockQuoteElement
import gg.essential.elementa.components.UIBlock
import org.xml.sax.Attributes
import java.awt.Color

class MarkdownBlockquoteComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
qName: String, attributes: Attributes?
) : BlockQuoteElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
override fun drawBlock(x: Float, y: Float, height: Float, renderData: RenderData) {
UIBlock.drawBlockSized(
renderData.matrixStack,
Color.WHITE,
(x + 4f).toDouble(), y.toDouble(),
2.0, height.toDouble()
)
}

override fun getBlockWidth(layoutData: LayoutData?): Float {
return 4f + 2f + 10f
}
}
22 changes: 16 additions & 6 deletions src/main/java/dev/dediamondpro/minemark/LayoutConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,26 @@ public enum Alignment {
}

public static class SpacingConfig {
private final float textSpacing;
private final float textPadding;
private final float paragraphPadding;
private final float listPadding;
private final float listIndentSpacing;
private final float blockQuotePadding;

public SpacingConfig(float textSpacing, float paragraphPadding, float listPadding, float listIndentSpacing) {
this.textSpacing = textSpacing;
public SpacingConfig(float textPadding, float paragraphPadding, float listIndentSpacing, float listPadding, float blockQuotePadding) {
this.textPadding = textPadding;
this.paragraphPadding = paragraphPadding;
this.listPadding = listPadding;
this.listIndentSpacing = listIndentSpacing;
this.listPadding = listPadding;
this.blockQuotePadding = blockQuotePadding;
}

public SpacingConfig(float textPadding, float paragraphPadding, float listIndentSpacing) {
this(textPadding, paragraphPadding, listIndentSpacing, paragraphPadding, paragraphPadding);
}

public float getTextSpacing() {
return textSpacing;
public float getTextPadding() {
return textPadding;
}

public float getParagraphPadding() {
Expand All @@ -174,6 +180,10 @@ public float getListPadding() {
public float getListIndentSpacing() {
return listIndentSpacing;
}

public float getBlockQuotePadding() {
return blockQuotePadding;
}
}

public static class HeadingConfig {
Expand Down
63 changes: 57 additions & 6 deletions src/main/java/dev/dediamondpro/minemark/LayoutData.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class LayoutData {
private final ArrayList<Consumer<MarkDownElementPosition>> elementListeners = new ArrayList<>();
private MarkDownLine currentLine = new MarkDownLine(0f);
private final float maxWidth;
private boolean topSpacingLocked = false;
private boolean bottomSpacingLocked = false;

public LayoutData(float maxWidth) {
this.maxWidth = maxWidth;
Expand All @@ -16,8 +18,14 @@ public boolean isLineEmpty() {
return currentLine.width == 0f;
}

public boolean isLineOccupied() {
return currentLine.width != 0f;
}

public void nextLine() {
currentLine = new MarkDownLine(currentLine.getBottomY());
topSpacingLocked = false;
bottomSpacingLocked = false;
}

public MarkDownElementPosition addElement(LayoutConfig.Alignment alignment, float width, float height) {
Expand Down Expand Up @@ -79,6 +87,39 @@ public MarkDownLine getCurrentLine() {
return currentLine;
}

public void updateTopSpacing(float spacing) {
if (topSpacingLocked) return;
currentLine.topSpacing = Math.max(currentLine.topSpacing, spacing);
}

public void updateBottomSpacing(float spacing) {
if (bottomSpacingLocked) return;
currentLine.bottomSpacing = Math.max(currentLine.bottomSpacing, spacing);
}

public void updatePadding(float padding) {
updateTopSpacing(padding);
updateBottomSpacing(padding);
}

public void setTopSpacing(float spacing) {
if (topSpacingLocked) return;
currentLine.topSpacing = spacing;
}

public void setBottomSpacing(float spacing) {
if (bottomSpacingLocked) return;
currentLine.bottomSpacing = spacing;
}

public void lockTopSpacing() {
topSpacingLocked = true;
}

public void lockBottomSpacing() {
bottomSpacingLocked = true;
}

public static class MarkDownElementPosition {
private final MarkDownLine line;
private final float x;
Expand Down Expand Up @@ -121,7 +162,7 @@ public float getY() {
}

public float getBottomY() {
return line.getBottomY();
return line.getBottomY() - line.getBottomSpacing();
}

public float getHeight() {
Expand All @@ -132,10 +173,6 @@ public float getWidth() {
return width;
}

public float getLineHeight() {
return line.getHeight();
}

public boolean isInside(float x, float y) {
return x >= getX() && x <= getRightX() &&
y >= getY() && y <= getBottomY();
Expand All @@ -146,6 +183,8 @@ public static class MarkDownLine {
private final float y;
private float width = 0f;
private float height = 0f;
private float topSpacing = 0f;
private float bottomSpacing = 0f;

public MarkDownLine(float y) {
this.y = y;
Expand All @@ -160,11 +199,23 @@ public float getWidth() {
}

public float getHeight() {
return topSpacing + height + bottomSpacing;
}

public float getRawHeight() {
return height;
}

public float getBottomY() {
return y + height;
return y + getHeight();
}

public float getBottomSpacing() {
return bottomSpacing;
}

public float getTopSpacing() {
return topSpacing;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ protected void draw(float xOffset, float yOffset, float mouseX, float mouseY, R
protected void generateLayout(LayoutData layoutData) {
width = getWidth(layoutData);
height = getHeight(layoutData);
if ((!(this instanceof Inline) && !layoutData.isLineEmpty()) || layoutData.getX() + width > layoutData.getMaxWidth()) {
if ((!(this instanceof Inline) && layoutData.isLineOccupied()) || layoutData.getX() + width > layoutData.getMaxWidth()) {
layoutData.nextLine();
}
position = layoutData.addElement(layoutConfig.getAlignment(), width, height);
if (!(this instanceof Inline) && !layoutData.isLineEmpty()) {
if (!(this instanceof Inline) && layoutData.isLineOccupied()) {
layoutData.nextLine();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ protected void draw(float xOffset, float yOffset, float mouseX, float mouseY, R

@Override
protected void generateLayout(LayoutData layoutData) {
if (!(this instanceof Inline) && !layoutData.isLineEmpty()) {
if (!(this instanceof Inline) && layoutData.isLineOccupied()) {
layoutData.nextLine();
}
float padding = getPadding(layoutData);
if (padding != 0 && !(parent instanceof NoPadding)) {
layoutData.setLineHeight(padding);
layoutData.nextLine();
}
layoutData.updateTopSpacing(padding);
for (Element<L, R> child : children) {
child.generateLayout(layoutData);
}
if (!(this instanceof Inline) && !layoutData.isLineEmpty()) {
layoutData.nextLine();
}
if (padding != 0 && !(parent instanceof NoPadding)) {
layoutData.setLineHeight(padding);
layoutData.updateBottomSpacing(padding);
if(!(this instanceof Inline)) {
layoutData.nextLine();
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/dediamondpro/minemark/elements/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ protected void onMouseClicked(MouseButton button, float mouseX, float mouseY) {
}
}

public int getChildIndex(Element<L, R> element) {
return children.indexOf(element);
}

/**
* Build a tree of elements for debugging purposes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum Elements {
IMAGE(listOf("img")),
LIST_PARENT(listOf("ol", "ul")),
LIST_ELEMENT(listOf("li")),
HORIZONTAL_LINE(listOf("hr"))
HORIZONTAL_LINE(listOf("hr")),
BLOCKQUOTE(listOf("blockquote"))
;

public final List<String> tags;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.dediamondpro.minemark.elements.impl;

import dev.dediamondpro.minemark.LayoutConfig;
import dev.dediamondpro.minemark.LayoutData;
import dev.dediamondpro.minemark.elements.ChildBasedElement;
import dev.dediamondpro.minemark.elements.Element;
import dev.dediamondpro.minemark.elements.Inline;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.xml.sax.Attributes;

public abstract class BlockQuoteElement<L extends LayoutConfig, R> extends ChildBasedElement<L, R> implements Inline {
protected LayoutData.MarkDownElementPosition position;
protected float blockWidth;

public BlockQuoteElement(@NotNull L layoutConfig, @Nullable Element<L, R> parent, @NotNull String qName, @Nullable Attributes attributes) {
super(layoutConfig, parent, qName, attributes);
}

@Override
protected void draw(float xOffset, float yOffset, float mouseX, float mouseY, R renderData) {
drawBlock(xOffset + position.getX(), yOffset + position.getY(), position.getHeight(), renderData);
super.draw(xOffset + position.getX() + blockWidth, yOffset + position.getY(), mouseX, mouseY, renderData);
}

@Override
protected void generateLayout(LayoutData layoutData) {
blockWidth = getBlockWidth(layoutData);
if (layoutData.isLineOccupied()) {
layoutData.nextLine();
}
layoutData.updatePadding(layoutConfig.getSpacingConfig().getBlockQuotePadding());
LayoutData newLayoutData = new LayoutData(layoutData.getMaxWidth() - blockWidth);
super.generateLayout(newLayoutData);
position = layoutData.addElement(LayoutConfig.Alignment.LEFT, layoutData.getMaxWidth(), newLayoutData.getY() + newLayoutData.getLineHeight());
layoutData.nextLine();
}

public abstract void drawBlock(float x, float y, float height, R renderData);

public abstract float getBlockWidth(LayoutData layoutData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import dev.dediamondpro.minemark.LayoutData;
import dev.dediamondpro.minemark.elements.ChildBasedElement;
import dev.dediamondpro.minemark.elements.Element;
import dev.dediamondpro.minemark.elements.NoPadding;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.xml.sax.Attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public AlignmentElement(@NotNull L layoutConfig, @Nullable Element<L, R> parent,

@Override
protected void generateLayout(LayoutData layoutData) {
if (!layoutData.isLineEmpty()) layoutData.nextLine();
if (layoutData.isLineOccupied()) layoutData.nextLine();
super.generateLayout(layoutData);
}

Expand Down
Loading

0 comments on commit b1d2fc1

Please sign in to comment.