-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mdddj/1.3.0
添加antd 表单生成功能
- Loading branch information
Showing
14 changed files
with
373 additions
and
57 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
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/shop/itbug/salvorstool/action/GenerateAntdFormAction.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 shop.itbug.salvorstool.action | ||
|
||
import com.intellij.openapi.actionSystem.ActionUpdateThread | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import shop.itbug.salvorstool.dialog.GenerateAntdFormDialog | ||
import shop.itbug.salvorstool.tool.tryGetRsStructPsiElement | ||
|
||
|
||
///生成antd表单 | ||
class GenerateAntdFormAction : AnAction() { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
e.tryGetRsStructPsiElement()?.let { | ||
GenerateAntdFormDialog(e.project!!, it).show() | ||
} | ||
} | ||
|
||
override fun update(e: AnActionEvent) { | ||
e.presentation.isVisible = e.tryGetRsStructPsiElement() != null | ||
super.update(e) | ||
} | ||
|
||
override fun getActionUpdateThread(): ActionUpdateThread { | ||
return ActionUpdateThread.BGT | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/shop/itbug/salvorstool/dialog/GenerateAntdFormDialog.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,35 @@ | ||
package shop.itbug.salvorstool.dialog | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.DialogWrapper | ||
import com.intellij.ui.components.JBTabbedPane | ||
import com.intellij.ui.dsl.builder.panel | ||
import org.rust.lang.core.psi.impl.RsStructItemImpl | ||
import shop.itbug.salvorstool.tool.AntdFactory | ||
import shop.itbug.salvorstool.tool.MyFieldPsiElementManager | ||
import shop.itbug.salvorstool.tool.myManager | ||
import shop.itbug.salvorstool.widget.TypeJavaScriptEditor | ||
import java.awt.Dimension | ||
import javax.swing.JComponent | ||
|
||
///生成antd表单弹窗 | ||
class GenerateAntdFormDialog(project: Project, psiElement: RsStructItemImpl) : DialogWrapper(project, true) { | ||
|
||
|
||
private val tabview = JBTabbedPane() | ||
|
||
init { | ||
super.init() | ||
title = "Generate Antd Form" | ||
|
||
tabview.add("预览", TypeJavaScriptEditor(project, AntdFactory.generateAntdForm(psiElement))) | ||
} | ||
|
||
override fun createCenterPanel(): JComponent { | ||
return tabview | ||
} | ||
|
||
override fun getPreferredSize(): Dimension { | ||
return Dimension(500, super.getPreferredSize().height) | ||
} | ||
} |
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
112 changes: 112 additions & 0 deletions
112
src/main/kotlin/shop/itbug/salvorstool/tool/AntdFactory.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,112 @@ | ||
package shop.itbug.salvorstool.tool | ||
|
||
import org.rust.lang.core.psi.impl.RsStructItemImpl | ||
|
||
|
||
sealed class Rule | ||
data class AntdRequiredRule(val message: String, val required: Boolean) : Rule() | ||
|
||
private fun Rule.generateString(): String { | ||
return when (this) { | ||
is AntdRequiredRule -> "{ message: '$message', required: ${if (required) "true" else "false"}}" | ||
} | ||
} | ||
|
||
|
||
object AntdFactory { | ||
|
||
|
||
///生成form | ||
fun generateAntdForm(psiElement: RsStructItemImpl): String { | ||
val sb = StringBuilder() | ||
val jsModels = psiElement.myManager.jsModelList | ||
|
||
//1.添加参数 | ||
sb.appendLine( | ||
""" | ||
type Prop = { | ||
trigger?: JSX.Element | undefined, | ||
initValues?: PropInitValue | undefined | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
//2.添加模型 | ||
sb.appendLine("interface PropInitValue {") | ||
jsModels.forEach { | ||
sb.appendLine("\t\t${it.propTextString},") | ||
} | ||
sb.appendLine("}") | ||
|
||
//3.添加field | ||
val fieldSb = StringBuilder() | ||
|
||
jsModels.forEach { | ||
fieldSb.appendLine("\t\t" + generateFormItem(it)) | ||
} | ||
|
||
//4.添加最外层的包装 | ||
sb.appendLine(getTemp(fieldSb.toString())) | ||
|
||
return sb.toString() | ||
} | ||
|
||
///生成form item | ||
fun generateFormItem(model: MyFieldPsiElementManager.JsModel): String { | ||
val type = when (model.type) { | ||
JavascriptType.Number -> "ProFormDigit" | ||
JavascriptType.String -> "ProFormText" | ||
JavascriptType.Bool -> "ProFormSwitch" | ||
JavascriptType.Unknown -> "" | ||
} | ||
if (type.isEmpty()) { | ||
return "" | ||
} | ||
|
||
val rules = mutableListOf<Rule>() | ||
//添加rule | ||
if (!model.isOption) { | ||
rules.add(AntdRequiredRule(message = "请输入${model.comment ?: "内容"}", required = true)) | ||
} | ||
|
||
return "<$type name='${model.fieldName}' label='${model.comment ?: ""}' ${generateRulesText(rules)} \t\t/>" | ||
} | ||
|
||
///生成规则 | ||
private fun generateRulesText(rules: List<Rule>): String { | ||
if (rules.isEmpty()) { | ||
return "" | ||
} | ||
val sb = StringBuilder() | ||
sb.appendLine("rules={[") | ||
rules.forEach { | ||
sb.appendLine("\t\t\t\t\t" + it.generateString()) | ||
} | ||
sb.appendLine("\t\t\t]}") | ||
return sb.toString() | ||
} | ||
|
||
} | ||
|
||
|
||
///模板引擎 | ||
private fun getTemp(field: String): String = """ | ||
const AddOrUpdateForm: React.FC<Prop> = (props) => { | ||
let isUpdate = props.initValues !== undefined | ||
//提交数据 | ||
const onFinish = async (values: PropInitValue) => { | ||
if(isUpdate) { | ||
// Todo! 修改 | ||
}else{ | ||
// ToDo! 新增 | ||
} | ||
return true | ||
} | ||
return ( | ||
<ModalForm<PropInitValue> trigger={props.trigger} initialValues={props.initValues} onFinish={onFinish}> | ||
$field | ||
</ModalForm> | ||
); | ||
}; | ||
export {AddOrUpdateForm} | ||
""".trimIndent() |
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
Oops, something went wrong.