This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added missing MouseEvent coordinates (#560)
* Added missing MouseEvent coordinates, specifically pageX, pageY, offsetX, and offsetY. * Added JavascriptPlugin and DynamicImagePlugin.
- Loading branch information
1 parent
7a08079
commit 5cf7751
Showing
6 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package kweb.plugins.image | ||
|
||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import kweb.plugins.KwebPlugin | ||
import java.awt.image.BufferedImage | ||
import java.io.ByteArrayOutputStream | ||
import javax.imageio.ImageIO | ||
|
||
/** | ||
* Add multiple dynamically generated images files to your Kweb app routing using ByteArray generator functions. | ||
* | ||
* @property resourceFolder The relative logical path used for routing to the dynamic images. | ||
* @property fileNames The map of file names to ByteArray providers, called once every time the image is requested. | ||
* | ||
* @author toddharrison | ||
*/ | ||
class DynamicImagePlugin( | ||
private val resourceFolder: String, | ||
private val fileNames: Map<String, | ||
suspend () -> ByteArray> | ||
): KwebPlugin() { | ||
constructor(resourceFolder: String, fileName: String, byteProvider: suspend () -> ByteArray): | ||
this(resourceFolder, mapOf(fileName to byteProvider)) | ||
|
||
constructor(resourceFolder: String, fileNames: Map<String, suspend () -> BufferedImage>, format: String): | ||
this(resourceFolder, fileNames | ||
.mapValues { (_, value) -> suspend { toByteArray(value(), format) }}) | ||
|
||
constructor(resourceFolder: String, fileName: String, bufferedImageProvider: suspend () -> BufferedImage, format: String): | ||
this(resourceFolder, mapOf(fileName to bufferedImageProvider), format) | ||
|
||
override fun appServerConfigurator(routeHandler: Routing) { | ||
fileNames.forEach { (fileName, byteProvider) -> | ||
routeHandler.get("$resourceFolder/$fileName") { _ -> | ||
context.respondBytes { | ||
byteProvider() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun toByteArray(bufferedImage: BufferedImage, format: String): ByteArray { | ||
val byteArrayOutputStream = ByteArrayOutputStream() | ||
ImageIO.write(bufferedImage, format, byteArrayOutputStream) | ||
return byteArrayOutputStream.toByteArray() | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/kweb/plugins/javascript/JavascriptPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package kweb.plugins.javascript | ||
|
||
import kweb.plugins.KwebPlugin | ||
import kweb.plugins.staticFiles.ResourceFolder | ||
import kweb.plugins.staticFiles.StaticFilesPlugin | ||
import org.jsoup.nodes.Document | ||
|
||
/** | ||
* Add multiple JavaScript files to your Kweb app from your resources folder. | ||
* | ||
* @property resourceFolder The relative path to the folder in the src/main/resources folder where the .js files are located | ||
* @property fileNames The list of file names located in the resourceFolder which should be added to the website. Only files with suffix .js (case-insensitive) will be linked | ||
* | ||
* @author toddharrison | ||
*/ | ||
class JavascriptPlugin(private val resourceFolder: String, private val fileNames: Set<String>) : KwebPlugin(dependsOn = setOf(StaticFilesPlugin(ResourceFolder(resourceFolder), "/kweb_static/js"))) { | ||
constructor(resourceFolder: String, fileName: String) : this(resourceFolder, setOf(fileName)) | ||
|
||
override fun decorate(doc: Document) { | ||
fileNames.filter { f -> f.endsWith(".js", true) }.forEach { | ||
doc.head().appendElement("script") | ||
.attr("type", "text/javascript") | ||
.attr("src", "/kweb_static/js/$it") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters