Skip to content

Commit

Permalink
Fixing Cui.Utilities.copyToClipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Mar 16, 2024
1 parent 5af6898 commit b476c46
Showing 1 changed file with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class SourceCodeComponentRenderer extends BaseDecoratorRenderer<SourceCod
static final String COPY_BTN_CLASS = "cui-btn-copy";
static final String COPY_BTN_WRAPPER_CLASS = "cui-btn-copy-wrapper";

static final String COPY_ON_CLICK_HANDLER = "icw.cui.CopyToClipboard('%s', '%s');return false;";
static final String COPY_ON_CLICK_HANDLER = "Cui.Utilities.copyToClipboard('%s', '%s');return false;";

static final String PRE_STYLE_CLASS = "prettyprint linenums";

Expand All @@ -65,24 +65,24 @@ public SourceCodeComponentRenderer() {

@Override
protected void doEncodeEnd(final FacesContext context, final DecoratingResponseWriter<SourceCodeComponent> writer,
final SourceCodeComponent component) throws IOException {
final SourceCodeComponent component) throws IOException {
if (!isEmpty(component.getDescription())) {
writer.withStartElement(Node.P);
writer.withTextContent(component.getDescription().formatted(), true);
writer.withEndElement(Node.P);
}
final var resolvedSource = component.resolveSource().replace("%", "%%").formatted().replace("%n",
LINE_SEPARATOR);
final var resolvedSource = component.resolveSource().replace("%", "%%").replace("%n",
LINE_SEPARATOR);
if (component.isEnableClipboard()) {
renderCopyElements(writer, resolvedSource);
}
writer.withStartElement(Node.PRE).withClientId(ID_PRE_WRAPPER)
.withStyleClass(component.getStyleClassBuilder().append(PRE_STYLE_CLASS)).withPassThroughAttributes()
.withAttributeStyle(component.getStyle());
.withStyleClass(component.getStyleClassBuilder().append(PRE_STYLE_CLASS)).withPassThroughAttributes()
.withAttributeStyle(component.getStyle());

writer.withStartElement(Node.CODE).withStyleClass(component.getType()).withTextContent(
replaceHtmlEntities(limitLineSize(sanitizeLineBreaks(resolvedSource), component.getMaxLineLength())),
false);
replaceHtmlEntities(limitLineSize(sanitizeLineBreaks(resolvedSource), component.getMaxLineLength())),
false);

writer.withEndElement(Node.CODE).withEndElement(Node.PRE);
}
Expand Down Expand Up @@ -130,8 +130,7 @@ static void handleLineSplit(final List<String> result, final String line, final
var newLineBuilder = new StringBuilder(indentString);
var firstLine = true;
for (final String element : splitted) {
if (newLineBuilder.length() + element.length() < rowLength + 1) {
} else {
if (newLineBuilder.length() + element.length() >= rowLength + 1) {
final var builtString = newLineBuilder.toString();
if (!builtString.trim().isEmpty()) {
result.add(MoreStrings.stripEnd(builtString, " "));
Expand Down Expand Up @@ -166,27 +165,27 @@ private static int computeIndent(final String line) {
/**
* Renders the copy button and the corresponding (invisible) textarea
*
* @param writer
* @param resolvedSource
* @throws IOException
* @param writer to be written to
* @param resolvedSource to be written
* @throws IOException pass through
*/
private static void renderCopyElements(final DecoratingResponseWriter<SourceCodeComponent> writer,
final String resolvedSource) throws IOException {
final String resolvedSource) throws IOException {
final var copyButtonId = writer.getComponentWrapper().getSuffixedClientId(COPY_BTN_IDENTIFIER);

final var invisibleTextArea = writer.getComponentWrapper().getSuffixedClientId(ID_INVISIBLE_TEXT_AREA_SUFFIX);

writer.withStartElement(Node.DIV).withStyleClass(COPY_BTN_WRAPPER_CLASS);
writer.withStartElement(Node.BUTTON).withClientId(COPY_BTN_IDENTIFIER).withStyleClass(COPY_BTN_CLASS)
.withAttributeTitle(COPY_BUTTON_TITLE).withAttribute(AttributeName.DATA_TOGGLE, AttributeValue.TOOLTIP);
.withAttributeTitle(COPY_BUTTON_TITLE).withAttribute(AttributeName.DATA_TOGGLE, AttributeValue.TOOLTIP);
writer.withAttribute(AttributeName.JS_ON_CLICK,
COPY_ON_CLICK_HANDLER.formatted(invisibleTextArea, copyButtonId));
COPY_ON_CLICK_HANDLER.formatted(invisibleTextArea, copyButtonId));

writer.withStartElement(Node.SPAN).withTextContent(COPY_BUTTON_LABEL, true);
writer.withEndElement(Node.SPAN).withEndElement(Node.BUTTON).withEndElement(Node.DIV);

writer.withStartElement(Node.TEXT_AREA).withClientId(ID_INVISIBLE_TEXT_AREA_SUFFIX)
.withAttributeStyle(AttributeValue.STYLE_DISPLAY_NONE.getContent());
.withAttributeStyle(AttributeValue.STYLE_DISPLAY_NONE.getContent());

writer.withTextContent(resolvedSource, true);
writer.withEndElement(Node.TEXT_AREA);
Expand Down

0 comments on commit b476c46

Please sign in to comment.