Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with Prism API plugin #72

Merged
merged 5 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ THE SOFTWARE.
<groupId>io.jenkins.plugins</groupId>
<artifactId>ionicons-api</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>prism-api</artifactId>
<version>1.29.0-7-rc295.1064eb_c55f37</version>
timja marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<!-- For some reason need to explicitly depend on commons-text-api and commons-lang3-api for plugin-util-api to load -->
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-text-api</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-lang3-api</artifactId>
</dependency>
Comment on lines +93 to +101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use prism 1.29.0-8 as local dependency I do not see such a problem. Where exactly did you see the problem? I've seen that in the BOM builds Mark also had this problem.

Suggested change
<!-- For some reason need to explicitly depend on commons-text-api and commons-lang3-api for plugin-util-api to load -->
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-text-api</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-lang3-api</artifactId>
</dependency>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure but lots of people were complaining about it for checks-api, and I added them there too:
jenkinsci/checks-api-plugin#233

Although at least in checks api it actually used those dependencies.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails injected tests from maven hpi plugin, if you try locally you will get:

[ERROR] Failures:
[ERROR]   InjectedTest.testPluginActive While testing design-library, plugin-util-api failed to start
Caused by: java.io.IOException: Failed to load: Plugin Utilities API Plugin (plugin-util-api 3.3.0)
 - Plugin is missing: commons-text-api (1.10.0-36.vc008c8fcda_7b_)
 - Plugin is missing: commons-lang3-api (3.12.0-36.vd97de6465d5b_)
	at hudson.PluginWrapper.resolvePluginDependencies(PluginWrapper.java:988)
	at hudson.PluginManager$2$1$1.run(PluginManager.java:555)
	at org.jvnet.hudson.reactor.TaskGraphBuilder$TaskImpl.run(TaskGraphBuilder.java:177)
	at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:305)
	at jenkins.model.Jenkins$5.runTask(Jenkins.java:1170)
	at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:221)
	at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:120)
	at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import hudson.model.Action;
import hudson.model.Describable;

import io.jenkins.plugins.prism.PrismConfiguration;
import java.util.ArrayList;
import java.util.List;

import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* @author Kohsuke Kawaguchi
Expand All @@ -34,6 +37,11 @@ public UISampleDescriptor getDescriptor() {
return (UISampleDescriptor) Jenkins.get().getDescriptorOrDie(getClass());
}

@Restricted(NoExternalUse.class)
public PrismConfiguration getPrismConfiguration() {
return PrismConfiguration.getInstance();
}

public static List<UISample> getAll() {
return new ArrayList<>(Jenkins.get().getExtensionList(UISample.class));
}
Expand Down
47 changes: 25 additions & 22 deletions src/main/resources/io/jenkins/plugins/designlibrary/sample.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
document.addEventListener("DOMContentLoaded", () => {
function extractLanguageFromClassList(element) {
return Array.from(element.classList)
.filter(clazz => clazz.startsWith('language-'))
.map(clazz => clazz.replace('language-', ''));
}

const url = document.querySelector('head').dataset.rooturl

Expand All @@ -17,34 +12,42 @@ document.addEventListener("DOMContentLoaded", () => {
fetch(fullUrl)
.then(response => response.text())
.then(text => {
const language = extractLanguageFromClassList(element);
if (language.length > 0) {
element.innerHTML = Prism.highlight(text, Prism.languages[language], language.pop())
} else {
element.innerHTML = text
element.innerText = text

Prism.highlightElement(element)

function setPrismBackgroundVariable() {
const computedStyle = window.getComputedStyle(element.parentElement)
const background = computedStyle.getPropertyValue('background')

document.documentElement.style
.setProperty(prismVariable, background);
}

const prismVariable = '--prism-background'
if (!getComputedStyle(document.documentElement).getPropertyValue(prismVariable)) {
setPrismBackgroundVariable()

if (window.isSystemRespectingTheme) {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
setPrismBackgroundVariable()
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be sufficient to call Prism.highlightElement(element) when the user changes the theme?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I've added some comments to explain more but the above isn't for the elements it's so that the copy to clipboard section matches which doesn't use prism:
image

}

const codeWrapper = element.closest(".jdl-component-code");
if (codeWrapper) {
const copyButton = codeWrapper.querySelector(".copy-button, .jenkins-copy-button")
copyButton.setAttribute("text", text)
}
});
if (executable === "true") {
var script = document.createElement("script"); // create a script DOM node
script.src = fullUrl; // set its src to the provided URL
document.head.appendChild(script);
const script = document.createElement("script"); // create a script DOM node
script.src = fullUrl; // set its src to the provided URL
document.head.appendChild(script);
}
})

document.querySelectorAll('.language-java,.language-xml,.language-html,.language-css')
.forEach(element => {
const language = extractLanguageFromClassList(element);

if (language.length > 0) {
element.innerHTML = Prism.highlight(element.innerHTML, Prism.languages[language], language.pop())
}
});

const shareButton = document.querySelector("#button-share");

if (shareButton) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/lib/samples/sample.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:s="/lib/samples" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout">
<j:jelly xmlns:j="jelly:core" xmlns:s="/lib/samples" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:p="/prism">
<st:documentation>
Page layout for Design Library

Expand All @@ -41,10 +41,11 @@ THE SOFTWARE.
</j:if>

<l:layout title="${title}">
<p:prism configuration="${it.prismConfiguration}" />

<st:adjunct includes="io.jenkins.plugins.designlibrary.styles" />
<st:adjunct includes="io.jenkins.plugins.designlibrary.sample" />
<st:adjunct includes="io.jenkins.plugins.designlibrary.preCode" />
<st:adjunct includes="io.jenkins.plugins.designlibrary.prism" />

<st:include page="header" optional="true" />

Expand Down
54 changes: 0 additions & 54 deletions src/main/resources/scss/abstracts/_overrides.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,6 @@
}
}

// Remove when code blocks follows user's theme
// https://github.com/jenkinsci/design-library-plugin/issues/68
pre {
position: relative;
margin: 0 0 var(--jdl-spacing) 0 !important;
padding: calc(var(--jdl-spacing) / 2) !important;
background: transparent !important;
z-index: 0;

&::before {
content: "";
position: absolute;
inset: 0;
background: var(--text-color);
border-radius: inherit;
opacity: 0.05;
z-index: -1;
}

code {
font-size: 1rem !important;
text-shadow: none !important;
color: var(--text-color) !important;

.token.cdata, .token.comment, .token.doctype, .token.prolog {
color: var(--text-color-secondary) !important;
}

.token.punctuation {
color: var(--text-color-secondary) !important;
}

.token.boolean, .token.constant, .token.deleted, .token.number, .token.property, .token.symbol, .token.tag {
color: var(--pink) !important;
}

.token.attr-name, .token.builtin, .token.char, .token.inserted, .token.selector, .token.string {
color: var(--green) !important;
}

.language-css .token.string, .style .token.string, .token.entity, .token.operator, .token.url {
color: var(--brown) !important;
}

.token.atrule, .token.attr-value, .token.keyword {
color: var(--cyan) !important;
}

.token.class-name, .token.function {
color: var(--purple) !important;
}
}
}

code {
font-family: var(--font-family-mono) !important;
color: var(--text-color-secondary);
Expand Down
11 changes: 1 addition & 10 deletions src/main/resources/scss/components/_source-block.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ $jdl-component-code-controls-spacing: 0.75rem;
position: absolute;
inset: 0;
border-radius: inherit;
background: var(--button-background);
background: var(--prism-background);
z-index: -1;
opacity: 0.75;
}

&__code {
Expand All @@ -37,14 +36,6 @@ $jdl-component-code-controls-spacing: 0.75rem;
flex-direction: column;
height: 100%;
padding: $jdl-component-code-controls-spacing;

& > div {
position: sticky;
top: calc(43px + #{$jdl-component-code-controls-spacing});
right: $jdl-component-code-controls-spacing;
display: flex;
gap: $jdl-component-code-controls-spacing;
}
}

&:not(:hover) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/Validation/sample.jelly
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<f:entry title="${%State name}" field="name">
<f:textbox/>
<f:textbox />
</f:entry>