feat:字体选择
This commit is contained in:
+13
@@ -27,6 +27,19 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="menu-divider"></div>
|
<div class="menu-divider"></div>
|
||||||
<div class="menu-item">
|
<div class="menu-item">
|
||||||
|
<div class="menu-item__font">
|
||||||
|
<span class="select">微软雅黑</span>
|
||||||
|
<div class="options">
|
||||||
|
<ul>
|
||||||
|
<li data-family='Yahei'>微软雅黑</li>
|
||||||
|
<li data-family="宋体">宋体</li>
|
||||||
|
<li data-family="黑体">黑体</li>
|
||||||
|
<li data-family="Arial">Arial</li>
|
||||||
|
<li data-family="Fantasy">Fantasy</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="menu-item__size-add">
|
<div class="menu-item__size-add">
|
||||||
<i></i>
|
<i></i>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export class Command {
|
|||||||
private static redo: Function
|
private static redo: Function
|
||||||
private static painter: Function
|
private static painter: Function
|
||||||
private static format: Function
|
private static format: Function
|
||||||
|
private static font: Function
|
||||||
private static sizeAdd: Function
|
private static sizeAdd: Function
|
||||||
private static sizeMinus: Function
|
private static sizeMinus: Function
|
||||||
private static bold: Function
|
private static bold: Function
|
||||||
@@ -22,6 +23,7 @@ export class Command {
|
|||||||
Command.redo = adapt.redo.bind(adapt)
|
Command.redo = adapt.redo.bind(adapt)
|
||||||
Command.painter = adapt.painter.bind(adapt)
|
Command.painter = adapt.painter.bind(adapt)
|
||||||
Command.format = adapt.format.bind(adapt)
|
Command.format = adapt.format.bind(adapt)
|
||||||
|
Command.font = adapt.font.bind(adapt)
|
||||||
Command.sizeAdd = adapt.sizeAdd.bind(adapt)
|
Command.sizeAdd = adapt.sizeAdd.bind(adapt)
|
||||||
Command.sizeMinus = adapt.sizeMinus.bind(adapt)
|
Command.sizeMinus = adapt.sizeMinus.bind(adapt)
|
||||||
Command.bold = adapt.bold.bind(adapt)
|
Command.bold = adapt.bold.bind(adapt)
|
||||||
@@ -51,7 +53,11 @@ export class Command {
|
|||||||
return Command.format()
|
return Command.format()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色
|
// 字体、字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色
|
||||||
|
public executeFont(payload: string) {
|
||||||
|
return Command.font(payload)
|
||||||
|
}
|
||||||
|
|
||||||
public executeSizeAdd() {
|
public executeSizeAdd() {
|
||||||
return Command.sizeAdd()
|
return Command.sizeAdd()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,15 @@ export class CommandAdapt {
|
|||||||
this.draw.render({ isSetCursor: false })
|
this.draw.render({ isSetCursor: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public font(payload: string) {
|
||||||
|
const selection = this.range.getSelection()
|
||||||
|
if (!selection) return
|
||||||
|
selection.forEach(el => {
|
||||||
|
el.font = payload
|
||||||
|
})
|
||||||
|
this.draw.render({ isSetCursor: false })
|
||||||
|
}
|
||||||
|
|
||||||
public sizeAdd() {
|
public sizeAdd() {
|
||||||
const selection = this.range.getSelection()
|
const selection = this.range.getSelection()
|
||||||
if (!selection) return
|
if (!selection) return
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ export class RangeManager {
|
|||||||
}
|
}
|
||||||
const curElement = curElementList[curElementList.length - 1]
|
const curElement = curElementList[curElementList.length - 1]
|
||||||
// 富文本
|
// 富文本
|
||||||
|
const font = curElement.font || this.options.defaultFont
|
||||||
let bold = !~curElementList.findIndex(el => !el.bold)
|
let bold = !~curElementList.findIndex(el => !el.bold)
|
||||||
let italic = !~curElementList.findIndex(el => !el.italic)
|
let italic = !~curElementList.findIndex(el => !el.italic)
|
||||||
let underline = !~curElementList.findIndex(el => !el.underline)
|
let underline = !~curElementList.findIndex(el => !el.underline)
|
||||||
@@ -66,6 +67,7 @@ export class RangeManager {
|
|||||||
undo,
|
undo,
|
||||||
redo,
|
redo,
|
||||||
painter,
|
painter,
|
||||||
|
font,
|
||||||
bold,
|
bold,
|
||||||
italic,
|
italic,
|
||||||
underline,
|
underline,
|
||||||
@@ -77,6 +79,7 @@ export class RangeManager {
|
|||||||
|
|
||||||
public recoveryRangeStyle() {
|
public recoveryRangeStyle() {
|
||||||
if (!this.listener.rangeStyleChange) return
|
if (!this.listener.rangeStyleChange) return
|
||||||
|
const font = this.options.defaultFont
|
||||||
const painter = !!this.draw.getPainterStyle()
|
const painter = !!this.draw.getPainterStyle()
|
||||||
const undo = this.historyManager.isCanUndo()
|
const undo = this.historyManager.isCanUndo()
|
||||||
const redo = this.historyManager.isCanRedo()
|
const redo = this.historyManager.isCanRedo()
|
||||||
@@ -84,6 +87,7 @@ export class RangeManager {
|
|||||||
undo,
|
undo,
|
||||||
redo,
|
redo,
|
||||||
painter,
|
painter,
|
||||||
|
font,
|
||||||
bold: false,
|
bold: false,
|
||||||
italic: false,
|
italic: false,
|
||||||
underline: false,
|
underline: false,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ export interface IRangeStype {
|
|||||||
undo: boolean;
|
undo: boolean;
|
||||||
redo: boolean;
|
redo: boolean;
|
||||||
painter: boolean;
|
painter: boolean;
|
||||||
|
font: string;
|
||||||
bold: boolean;
|
bold: boolean;
|
||||||
italic: boolean;
|
italic: boolean;
|
||||||
underline: boolean;
|
underline: boolean;
|
||||||
|
|||||||
+16
-1
@@ -78,7 +78,18 @@ window.onload = function () {
|
|||||||
instance.command.executeFormat()
|
instance.command.executeFormat()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色
|
// 字体、字体变大、字体变小、加粗、斜体、下划线、删除线、字体颜色、背景色
|
||||||
|
const fontDom = document.querySelector<HTMLDivElement>('.menu-item__font')!
|
||||||
|
const fontSelectDom = fontDom.querySelector<HTMLDivElement>('.select')!
|
||||||
|
const fontOptionDom = fontDom.querySelector<HTMLDivElement>('.options')!
|
||||||
|
fontDom.onclick = function () {
|
||||||
|
console.log('font')
|
||||||
|
fontOptionDom.classList.toggle('visible')
|
||||||
|
}
|
||||||
|
fontOptionDom.onclick = function (evt) {
|
||||||
|
const li = evt.target as HTMLLIElement
|
||||||
|
instance.command.executeFont(li.dataset.family!)
|
||||||
|
}
|
||||||
document.querySelector<HTMLDivElement>('.menu-item__size-add')!.onclick = function () {
|
document.querySelector<HTMLDivElement>('.menu-item__size-add')!.onclick = function () {
|
||||||
console.log('size-add')
|
console.log('size-add')
|
||||||
instance.command.executeSizeAdd()
|
instance.command.executeSizeAdd()
|
||||||
@@ -156,6 +167,10 @@ window.onload = function () {
|
|||||||
// 内部事件监听
|
// 内部事件监听
|
||||||
instance.listener.rangeStyleChange = function (payload) {
|
instance.listener.rangeStyleChange = function (payload) {
|
||||||
// 富文本
|
// 富文本
|
||||||
|
const curFontDom = fontOptionDom.querySelector<HTMLLIElement>(`[data-family=${payload.font}]`)!
|
||||||
|
fontSelectDom.innerText = curFontDom.innerText
|
||||||
|
fontOptionDom.querySelectorAll<HTMLLIElement>('li').forEach(li => li.classList.remove('active'))
|
||||||
|
curFontDom.classList.add('active')
|
||||||
payload.bold ? boldDom.classList.add('active') : boldDom.classList.remove('active')
|
payload.bold ? boldDom.classList.add('active') : boldDom.classList.remove('active')
|
||||||
payload.italic ? italicDom.classList.add('active') : italicDom.classList.remove('active')
|
payload.italic ? italicDom.classList.add('active') : italicDom.classList.remove('active')
|
||||||
payload.underline ? underlineDom.classList.add('active') : underlineDom.classList.remove('active')
|
payload.underline ? underlineDom.classList.add('active') : underlineDom.classList.remove('active')
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ body {
|
|||||||
background-color: #F2F4F7;
|
background-color: #F2F4F7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ul {
|
||||||
|
list-style: none;
|
||||||
|
}
|
||||||
|
|
||||||
.menu {
|
.menu {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 60px;
|
height: 60px;
|
||||||
@@ -68,6 +72,67 @@ body {
|
|||||||
border: 1px solid #e2e6ed;
|
border: 1px solid #e2e6ed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.menu-item .select {
|
||||||
|
border: none;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 24px;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item .select::after {
|
||||||
|
position: absolute;
|
||||||
|
content: "";
|
||||||
|
top: 11px;
|
||||||
|
width: 0;
|
||||||
|
height: 0;
|
||||||
|
right: 2px;
|
||||||
|
border-color: #767c85 transparent transparent;
|
||||||
|
border-style: solid solid none;
|
||||||
|
border-width: 3px 3px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item .options {
|
||||||
|
width: 70px;
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
top: 25px;
|
||||||
|
padding: 10px;
|
||||||
|
background: #fff;
|
||||||
|
font-size: 14px;
|
||||||
|
box-shadow: 0 2px 12px 0 rgb(56 56 56 / 20%);
|
||||||
|
border: 1px solid #e2e6ed;
|
||||||
|
border-radius: 2px;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item .options.visible {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item .options li {
|
||||||
|
padding: 5px;
|
||||||
|
margin: 5px 0;
|
||||||
|
transition: all .3s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item .options li:hover {
|
||||||
|
background-color: #ebecef;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item .options li.active {
|
||||||
|
background-color: #e2e6ed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item .menu-item__font {
|
||||||
|
width: 65px;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item__font .select {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
.menu-item__undo.no-allow,
|
.menu-item__undo.no-allow,
|
||||||
.menu-item__redo.no-allow {
|
.menu-item__redo.no-allow {
|
||||||
color: #c0c4cc;
|
color: #c0c4cc;
|
||||||
|
|||||||
Reference in New Issue
Block a user