CodeExpander.qml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import QtQuick
  2. import QtQuick.Layouts
  3. import QtQuick.Window
  4. import QtQuick.Controls
  5. import FluentUI
  6. FluExpander{
  7. property string code: ""
  8. headerText: "Source"
  9. contentHeight:content.height
  10. FluMultilineTextBox{
  11. id:content
  12. width:parent.width
  13. readOnly:true
  14. text:highlightQmlCode(code)
  15. focus:false
  16. textFormat: FluMultilineTextBox.RichText
  17. KeyNavigation.priority: KeyNavigation.BeforeItem
  18. background:Rectangle{
  19. radius: 4
  20. color:FluTheme.dark ? Qt.rgba(50/255,50/255,50/255,1) : Qt.rgba(247/255,247/255,247/255,1)
  21. border.color: FluTheme.dark ? Qt.rgba(45/255,45/255,45/255,1) : Qt.rgba(226/255,229/255,234/255,1)
  22. border.width: 1
  23. }
  24. }
  25. FluIconButton{
  26. iconSource:FluentIcons.Copy
  27. anchors{
  28. right: parent.right
  29. top: parent.top
  30. rightMargin: 5
  31. topMargin: 5
  32. }
  33. onClicked:{
  34. FluTools.clipText(content.text)
  35. showSuccess("复制成功")
  36. }
  37. }
  38. function htmlEncode(e){
  39. var i,s;
  40. for(i in s={
  41. "&":/&/g,//""//":/"/g,"'":/'/g,
  42. "<":/</g,">":/>/g,"<br/>":/\n/g,
  43. " ":/ /g," ":/\t/g
  44. })e=e.replace(s[i],i);
  45. return e;
  46. }
  47. function highlightQmlCode(code) {
  48. // 定义 QML 关键字列表
  49. var qmlKeywords = [
  50. "FluTextButton",
  51. "FluAppBar",
  52. "FluAutoSuggestBox",
  53. "FluBadge",
  54. "FluButton",
  55. "FluCalendarPicker",
  56. "FluCalendarView",
  57. "FluCarousel",
  58. "FluCheckBox",
  59. "FluColorPicker",
  60. "FluColorView",
  61. "FluComboBox",
  62. "FluContentDialog",
  63. "FluContentPage",
  64. "FluDatePicker",
  65. "FluDivider",
  66. "FluDropDownButton",
  67. "FluExpander",
  68. "FluFilledButton",
  69. "FluFlipView",
  70. "FluFocusRectangle",
  71. "FluIcon",
  72. "FluIconButton",
  73. "FluInfoBar",
  74. "FluItem",
  75. "FluMediaPlayer",
  76. "FluMenu",
  77. "FluMenuItem",
  78. "FluMultilineTextBox",
  79. "FluNavigationView",
  80. "FluObject",
  81. "FluPaneItem",
  82. "FluPaneItemExpander",
  83. "FluPaneItemHeader",
  84. "FluPaneItemSeparator",
  85. "FluPivot",
  86. "FluPivotItem",
  87. "FluProgressBar",
  88. "FluProgressRing",
  89. "FluRadioButton",
  90. "FluRectangle",
  91. "FluScrollablePage",
  92. "FluScrollBar",
  93. "FluShadow",
  94. "FluSlider",
  95. "FluTabView",
  96. "FluText",
  97. "FluTextArea",
  98. "FluTextBox",
  99. "FluTextBoxBackground",
  100. "FluTextBoxMenu",
  101. "FluTextButton",
  102. "FluTextFiled",
  103. "FluTimePicker",
  104. "FluToggleSwitch",
  105. "FluTooltip",
  106. "FluTreeView",
  107. "FluWindow",
  108. "FluWindowResize",
  109. "FluToggleButton",
  110. "FluTableView",
  111. "FluColors",
  112. "FluTheme",
  113. "FluStatusView",
  114. "FluRatingControl",
  115. "FluPasswordBox",
  116. "FluBreadcrumbBar",
  117. "FluCopyableText"
  118. ];
  119. code = code.replace(/\n/g, "<br>");
  120. code = code.replace(/ /g, "&nbsp;");
  121. return code.replace(RegExp("\\b(" + qmlKeywords.join("|") + ")\\b", "g"), "<span style='color: #c23a80'>$1</span>");
  122. }
  123. }