Kotlin 交互演示
设置
在你的项目中安装 kotlin-playground:
pnpm add -D kotlin-playgroundyarn add -D kotlin-playgroundnpm i -D kotlin-playground之后启用它:
import { hopeTheme } from "vuepress-theme-hope";
export default hopeTheme({
markdown: {
kotlinPlayground: true,
},
});使用
为了使用 kotlin 交互演示,你应该使用一个名为 kotlin-playground 的容器。
在其中,你可以使用 2 个指令:
@file 文件名紧跟文件的代码块第一个文件将是主文件,其他文件将被视为隐藏的依赖项。
@setting紧跟一个自定义设置的 json 块支持的选项:
data-version: 目标 Kotlin 编译器版本data-min-compiler-version: 最低目标 Kotlin 编译器版本data-target-platform: 目标平台,可以是junit,canvas,js或java(默认)。data-highlight-only: 只读模式,仅高亮。data-highlight-only: "nocursor"表示不在编辑器上聚焦。或者,你可以通过将其放置在
//sampleStart和//sampleEnd标记之间来使代码的一部分只读。如果你不需要此功能,请设置none-markers。data-js-libs: 默认情况下,组件会加载 jQuery 并使其可用于在编辑器中运行的代码。如果你需要任何其他 JS 库,请将它们指定为此属性中的逗号分隔列表。data-output-height: 在输出中以 px 设置 iframe 高度。用于目标平台画布,默认值为200。data-crosslink: 显示在 playground 中打开的链接,可以是enabled或disabled,默认为undefined- 仅在 playground 中支持。data-shorter-height: 如果高度大于属性值,则显示展开器,默认值为100。data-scrollbar-style: 选择滚动条实现,默认为overlay。args: 命令行参数。folded-button: 设置为 false 以隐藏代码片段auto-indent: 是否使用上下文敏感缩进,可以是true或false(默认)。theme: 编辑器 IntelliJ IDEA 主题,可以是idea,darculadefault。mode: 不同的语言风格。只有 kotlin 可以运行片段,可以是kotlin(默认),js,java,groovy,xml,c,shell,swift,obj-c。autocomplete每次按键都会自动完成,可以是true或false(默认)。如果为 false => 按ctrl+space激活自动完成。highlight-on-fly: 每次更改编辑器时都会检查错误和警告,可以是true或false(默认)。indent: 一个块应缩进多少个空格。默认为4。lines: 是否在编辑器左侧显示行号,可以是true或false(默认)。from和to: 创建代码的一部分。例如从第 5 行到第 10 行。match-brackets: 确定每当光标移动到括号旁边时是否匹配括号,可以是true或false(默认)。
你可以查看下面的演示以了解更多细节。
你可以在客户端配置文件中导入并调用 defineKotlinPlaygroundConfig 来自定义 kotlin-playground:
import { defineKotlinPlaygroundConfig } from "vuepress-plugin-md-enhance/client";
defineKotlinPlaygroundConfig({
// `kotlin-playground` options here
});interface KotlinPlaygroundOptions {
server?: string;
version?: string;
onChange?: (code: string) => void;
onRun?: () => void;
onError?: () => void;
getJsCode?: (code: string) => void;
onTestPassed?: () => void;
onTestFailed?: () => void;
onOpenConsole?: () => void;
onCloseConsole?: () => void;
callback?: (targetNode: HTMLElement, mountNode: HTMLElement) => void;
getInstance?: (instance: KotlinPlaygroundInstance) => void;
}案例
class Contact(val id: Int, var email: String)
fun main(args: Array<String>) {
val contact = Contact(1, "mary@gmail.com")
println(contact.id)
}
::: kotlin-playground 交互演示标题
@file main.kt
```kotlin
class Contact(val id: Int, var email: String)
fun main(args: Array<String>) {
val contact = Contact(1, "mary@gmail.com")
println(contact.id)
}
```
:::fun main(args: Array<String>) {
println("Hello World!")
}
::: kotlin-playground 只读演示
@file main.kt
```kotlin
fun main(args: Array<String>) {
println("Hello World!")
}
```
@settings
```json
{
"data-highlight-only": ""
}
```
:::import org.junit.Test
import org.junit.Assert
class TestExtensionFunctions() {
@Test fun testIntExtension() {
Assert.assertEquals("Rational number creation error: ", RationalNumber(4, 1), 4.r())
}
@Test fun testPairExtension() {
Assert.assertEquals("Rational number creation error: ", RationalNumber(2, 3), Pair(2, 3).r())
}
}
//sampleStart
/*
Then implement extension functions Int.r() and Pair.r() and make them convert Int and Pair to RationalNumber.
*/
fun Int.r(): RationalNumber = RationalNumber(this, 2)
fun Pair<Int, Int>.r(): RationalNumber = RationalNumber(first, second)
data class RationalNumber(val numerator: Int, val denominator: Int)
//sampleEnd
::: kotlin-playground 测试演示
@file main.kt
```kotlin
import org.junit.Test
import org.junit.Assert
class TestExtensionFunctions() {
@Test fun testIntExtension() {
Assert.assertEquals("Rational number creation error: ", RationalNumber(4, 1), 4.r())
}
@Test fun testPairExtension() {
Assert.assertEquals("Rational number creation error: ", RationalNumber(2, 3), Pair(2, 3).r())
}
}
//sampleStart
/*
Then implement extension functions Int.r() and Pair.r() and make them convert Int and Pair to RationalNumber.
*/
fun Int.r(): RationalNumber = RationalNumber(this, 2)
fun Pair<Int, Int>.r(): RationalNumber = RationalNumber(first, second)
data class RationalNumber(val numerator: Int, val denominator: Int)
//sampleEnd
```
@settings
```json
{
"data-target-platform": "junit"
}
```
:::external fun moment(): dynamic
fun main() {
val startOfDay = moment().startOf("day").fromNow()
println("The start of the day was $startOfDay")
}
::: kotlin-playground 外部 JS 库
@file main.kt
```kotlin
external fun moment(): dynamic
fun main() {
val startOfDay = moment().startOf("day").fromNow()
println("The start of the day was $startOfDay")
}
```
@settings
```json
{
"data-target-platform": "js",
"data-js-libs": "https://unpkg.com/moment@2"
}
```
:::import cat.Cat
fun main(args: Array<String>) {
//sampleStart
val cat = Cat("Kitty")
println(cat.name)
//sampleEnd
}
::: kotlin-playground 多个文件
@file main.kt
```kotlin
import cat.Cat
fun main(args: Array<String>) {
//sampleStart
val cat = Cat("Kitty")
println(cat.name)
//sampleEnd
}
```
@file cat.kt
```kotlin
package cat
class Cat(val name: String)
```
:::更新日志
05fbb-于1bd5f-于b1741-于55e92-于b1230-于1b917-于22787-于4c6db-于b41c0-于671db-于54c46-于05f91-于8a130-于68339-于4d282-于1669d-于49ab9-于d86cb-于55ea3-于792dc-于83bff-于48f71-于3a6d6-于86180-于71423-于8174c-于52ad0-于7c8cf-于