Browse Source

为富文本编辑器增加行距设置

在 CKEditor 工具栏增加“行距”下拉,支持默认、1.0、1.25、1.5、1.75、1.8、2.0、2.5、3.0 倍行距。行距以标准 line-height 样式写入正文,支持已有内容回显、批量应用到选中段落以及恢复默认。

Validation:
- node --check 通过
- 正式后台实际打开文章录入页,工具栏正常显示“行距”
- 选择 1.5 倍后生成 <p style="line-height:1.5;">,界面计算行高正确
- 恢复默认后 line-height 样式被移除
- 华夏正式生产环境内外网文件 SHA-256 一致
非繁 2 weeks ago
parent
commit
a10b88b500

+ 46 - 0
WebRoot/apps/space/css/space.editor.css

@@ -32,6 +32,52 @@
 	flex-wrap: wrap !important;
 }
 
+.space-editor-wrap .space-editor-line-height {
+	display: inline-flex;
+	align-items: center;
+	gap: 4px;
+	height: 38px;
+	margin: 1px;
+	padding: 0 6px;
+	border: 1px solid transparent;
+	border-radius: 2px;
+	color: #333;
+	font-size: 12px;
+	box-sizing: border-box;
+	vertical-align: top;
+}
+
+.space-editor-wrap .space-editor-line-height:hover,
+.space-editor-wrap .space-editor-line-height:focus-within {
+	border-color: #c7d8ea;
+	background: #f3f7fc;
+}
+
+.space-editor-wrap .space-editor-line-height-label {
+	font-size: 12px;
+	line-height: 1;
+	white-space: nowrap;
+}
+
+.space-editor-wrap .space-editor-line-height-select {
+	height: 26px;
+	min-width: 70px;
+	padding: 0 20px 0 5px;
+	border: 1px solid #c4c4c4;
+	border-radius: 2px;
+	background: #fff;
+	color: #333;
+	font-size: 12px;
+	line-height: 24px;
+	cursor: pointer;
+	outline: none;
+}
+
+.space-editor-wrap .space-editor-line-height-select:focus {
+	border-color: #6da5d5;
+	box-shadow: 0 0 0 2px rgba(109, 165, 213, 0.2);
+}
+
 .space-editor-toolstrip {
 	display: flex;
 	align-items: center;

+ 153 - 3
WebRoot/apps/space/js/space.vue.subassembly.js

@@ -1706,8 +1706,9 @@ Vue.component('spaceEditor', {
 					view: { key: 'style', value: { 'text-indent': '2em' } }
 				});
 			},
-			getSelectedTextBlocks: function (editor) {
+			getSelectedTextBlocks: function (editor, attributeName) {
 				var blocks = [];
+				var attribute = attributeName || 'textIndent';
 				var selection = editor.model.document.selection;
 				if (!selection.isCollapsed && typeof selection.getSelectedBlocks === 'function') {
 					blocks = Array.prototype.slice.call(selection.getSelectedBlocks());
@@ -1722,7 +1723,7 @@ Vue.component('spaceEditor', {
 				}
 
 				return blocks.filter(function (block) {
-					return block && editor.model.schema.checkAttribute(block.name, 'textIndent');
+					return block && editor.model.schema.checkAttribute(block.name, attribute);
 				});
 			},
 			toggleFirstLineIndent: function (editor) {
@@ -1835,6 +1836,152 @@ Vue.component('spaceEditor', {
 				});
 				this.refreshFirstLineIndentButton(editor);
 			},
+			installLineHeightPlugin: function (editor) {
+				if (!editor.model.schema.checkAttribute('$block', 'lineHeight')) {
+					editor.model.schema.extend('$block', { allowAttributes: ['lineHeight'] });
+				}
+				editor.model.schema.setAttributeProperties('lineHeight', { isFormatting: true });
+
+				editor.conversion.for('downcast').attributeToAttribute({
+					model: 'lineHeight',
+					view: function (modelAttributeValue) {
+						return {
+							key: 'style',
+							value: { 'line-height': modelAttributeValue }
+						};
+					}
+				});
+				editor.conversion.for('upcast').attributeToAttribute({
+					view: {
+						styles: { 'line-height': /[\s\S]+/ }
+					},
+					model: {
+						key: 'lineHeight',
+						value: function (viewElement) {
+							return viewElement.getStyle('line-height');
+						}
+					}
+				});
+			},
+			applyLineHeight: function (editor, value) {
+				var blocks = this.getSelectedTextBlocks(editor, 'lineHeight');
+				if (!blocks.length) {
+					return;
+				}
+
+				editor.model.change(function (writer) {
+					blocks.forEach(function (block) {
+						if (value) {
+							writer.setAttribute('lineHeight', value, block);
+						} else {
+							writer.removeAttribute('lineHeight', block);
+						}
+					});
+				});
+				editor.editing.view.focus();
+			},
+			refreshLineHeightControl: function (editor) {
+				if (!editor || !editor.ui || !editor.ui.view || !editor.ui.view.element) {
+					return;
+				}
+
+				var control = editor.ui.view.element.querySelector('[data-space-line-height="true"]');
+				if (!control) {
+					return;
+				}
+
+				var blocks = this.getSelectedTextBlocks(editor, 'lineHeight');
+				var current = '';
+				if (blocks.length) {
+					current = blocks[0].getAttribute('lineHeight') || '';
+					for (var i = 1; i < blocks.length; i++) {
+						if ((blocks[i].getAttribute('lineHeight') || '') !== current) {
+							current = '';
+							break;
+						}
+					}
+				}
+				control.value = current;
+			},
+			mountLineHeightToolbar: function (editor) {
+				if (!editor || !editor.ui || !editor.ui.view || !editor.ui.view.element) {
+					return;
+				}
+
+				var toolbarItems = editor.ui.view.element.querySelector('.ck-toolbar__items');
+				if (!toolbarItems || toolbarItems.querySelector('[data-space-line-height="true"]')) {
+					return;
+				}
+
+				var _this = this;
+				var wrapper = document.createElement('label');
+				wrapper.className = 'space-editor-line-height';
+				wrapper.setAttribute('title', '行距');
+
+				var label = document.createElement('span');
+				label.className = 'space-editor-line-height-label';
+				label.textContent = '行距';
+
+				var select = document.createElement('select');
+				select.className = 'space-editor-line-height-select';
+				select.setAttribute('aria-label', '行距');
+				select.setAttribute('data-space-line-height', 'true');
+				[
+					['', '默认'],
+					['1', '1.0 倍'],
+					['1.25', '1.25 倍'],
+					['1.5', '1.5 倍'],
+					['1.75', '1.75 倍'],
+					['1.8', '1.8 倍'],
+					['2', '2.0 倍'],
+					['2.5', '2.5 倍'],
+					['3', '3.0 倍']
+				].forEach(function (item) {
+					var option = document.createElement('option');
+					option.value = item[0];
+					option.textContent = item[1];
+					select.appendChild(option);
+				});
+
+				wrapper.appendChild(label);
+				wrapper.appendChild(select);
+				wrapper.addEventListener('mousedown', function (event) {
+					event.stopPropagation();
+				});
+				wrapper.addEventListener('click', function (event) {
+					event.stopPropagation();
+				});
+				select.addEventListener('change', function (event) {
+					event.stopPropagation();
+					_this.applyLineHeight(editor, select.value);
+				});
+
+				var fontSizeDropdown = null;
+				var dropdowns = toolbarItems.querySelectorAll('.ck-dropdown');
+				for (var i = 0; i < dropdowns.length; i++) {
+					var button = dropdowns[i].querySelector('.ck-button');
+					var buttonLabel = button ? (button.getAttribute('aria-label') || button.getAttribute('title') || button.textContent || '') : '';
+					buttonLabel = buttonLabel.replace(/\s/g, '').toLowerCase();
+					if (buttonLabel.indexOf('字体大小') !== -1 || buttonLabel.indexOf('字号') !== -1 || buttonLabel.indexOf('fontsize') !== -1) {
+						fontSizeDropdown = dropdowns[i];
+						break;
+					}
+				}
+
+				if (fontSizeDropdown && fontSizeDropdown.parentNode === toolbarItems) {
+					toolbarItems.insertBefore(wrapper, fontSizeDropdown.nextSibling);
+				} else {
+					toolbarItems.appendChild(wrapper);
+				}
+
+				editor.model.document.selection.on('change', function () {
+					_this.refreshLineHeightControl(editor);
+				});
+				editor.model.document.on('change:data', function () {
+					_this.refreshLineHeightControl(editor);
+				});
+				this.refreshLineHeightControl(editor);
+			},
 		alignFullScreenToolbarButton: function () {
 			if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
 				return;
@@ -3330,6 +3477,7 @@ Vue.component('spaceEditor', {
 				extraPlugins: [function (editor) {
 					_this.installUploadAdapter(editor);
 						_this.installFirstLineIndentPlugin(editor);
+						_this.installLineHeightPlugin(editor);
 					_this.installVideoViewerPlugin(editor);
 					_this.installPdfViewerPlugin(editor);
 				}], // 添加上传适配器
@@ -3342,6 +3490,7 @@ Vue.component('spaceEditor', {
 							'default',
 							{ title: '宋体', model: 'SimSun, serif', view: { name: 'span', styles: { 'font-family': "'SimSun', serif" }, priority: 7 } },
 							{ title: '仿宋', model: 'FangSong, serif', view: { name: 'span', styles: { 'font-family': "'FangSong', serif" }, priority: 7 } },
+							{ title: '仿宋_GB2312', model: "'仿宋_GB2312', 'FangSong_GB2312', FangSong, serif", view: { name: 'span', styles: { 'font-family': "'仿宋_GB2312', 'FangSong_GB2312', FangSong, serif" }, priority: 7 } },
 							{ title: '楷体', model: 'KaiTi, serif', view: { name: 'span', styles: { 'font-family': "'KaiTi', serif" }, priority: 7 } },
 							{ title: '黑体', model: 'SimHei, sans-serif', view: { name: 'span', styles: { 'font-family': "'SimHei', sans-serif" }, priority: 7 } },
 							{ title: '微软雅黑', model: 'Microsoft YaHei, sans-serif', view: { name: 'span', styles: { 'font-family': "'Microsoft YaHei', sans-serif" }, priority: 7 } },
@@ -3354,7 +3503,7 @@ Vue.component('spaceEditor', {
 					},
 					fontSize: {
 						supportAllValues: true,
-						options: ['default', '10.5pt', '12pt', '14pt', '16pt', '18pt', '22pt', '26pt', '36pt']
+						options: ['default', '10.5pt', '12pt', '14pt', '16pt', '18pt', '20pt', '22pt', '26pt', '36pt']
 					},
 				mediaEmbed: {
 					previewsInData: true,
@@ -3403,6 +3552,7 @@ Vue.component('spaceEditor', {
 				_this.editor = editor;
 				_this.alignFullScreenToolbarButton();
 					_this.mountFirstLineIndentToolbarButton(editor);
+					_this.mountLineHeightToolbar(editor);
 					_this.configureWordUploadPlugin(editor);
 				if (_this.type !== 'flow') {
 					_this.mountVideoUploadToolbarButton();

+ 2 - 2
WebRoot/web/subassembly/app/pc/10021/10104/1002110104_run.jsp

@@ -12,13 +12,13 @@
  <link rel="stylesheet" type="text/css" href="/apps/space/css/SpaceIcon/iconfont.css"> 
  <link rel="stylesheet" type="text/css" href="/css/components.css"> 
  <link rel="stylesheet" href="/css/ckeditor.css"> 
- <link rel="stylesheet" href="/apps/space/css/space.editor.css?v=20261027-fangsong-font-2">
+ <link rel="stylesheet" href="/apps/space/css/space.editor.css?v=20261110-line-height">
  <script>var space = { vars: {} };</script> 
  <script src="/js/jquery1.11.3.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261027-word-format-3"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261110-line-height"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261027-image-15m" type="text/javascript" charset="utf-8"></script>

+ 2 - 2
WebRoot/web/subassembly/app/pc/10021/10105/1002110105_run.jsp

@@ -12,13 +12,13 @@
  <link rel="stylesheet" type="text/css" href="/apps/space/css/SpaceIcon/iconfont.css"> 
  <link rel="stylesheet" type="text/css" href="/css/components.css"> 
  <link rel="stylesheet" href="/css/ckeditor.css"> 
- <link rel="stylesheet" href="/apps/space/css/space.editor.css?v=20261027-fangsong-font-2">
+ <link rel="stylesheet" href="/apps/space/css/space.editor.css?v=20261110-line-height">
  <script>var space = { vars: {} };</script> 
  <script src="/js/jquery1.11.3.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261027-word-format-3"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261110-line-height"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261027-image-15m" type="text/javascript" charset="utf-8"></script>

+ 2 - 2
WebRoot/web/subassembly/app/pc/10021/10211/1002110211_run.jsp

@@ -12,13 +12,13 @@
  <link rel="stylesheet" type="text/css" href="/apps/space/css/SpaceIcon/iconfont.css"> 
  <link rel="stylesheet" type="text/css" href="/css/components.css"> 
  <link rel="stylesheet" href="/css/ckeditor.css"> 
-		<link rel="stylesheet" href="/apps/space/css/space.editor.css?v=20261027-fangsong-font-2">
+		<link rel="stylesheet" href="/apps/space/css/space.editor.css?v=20261110-line-height">
  <script>var space = { vars: {} };</script> 
  <script src="/js/jquery1.11.3.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261027-word-format-3"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261110-line-height"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261027-image-15m" type="text/javascript" charset="utf-8"></script>