fix: getValue 获取光标未有cursor节点

This commit is contained in:
yanmao 2022-01-19 22:08:54 +08:00
parent 4ae53eae13
commit f75d8f76b4
7 changed files with 56 additions and 18 deletions

View File

@ -9,7 +9,7 @@ import { RangeInterface, RangePath } from '../types/range';
import ChangeEvent from './event';
import Parser from '../parser';
import { ANCHOR_SELECTOR, CURSOR_SELECTOR, FOCUS_SELECTOR } from '../constants';
import { combinText } from '../utils';
import { combinText, transformCustomTags } from '../utils';
import { TRIGGER_CARD_ID } from '../constants/card';
import { DATA_ID, EDITABLE_SELECTOR, UI_SELECTOR } from '../constants/root';
import { SelectionInterface } from '../types/selection';

View File

@ -8,6 +8,7 @@ import {
READY_CARD_KEY,
CARD_EDITABLE_KEY,
} from './card';
import { ANCHOR, CURSOR, FOCUS } from '.';
const defaultConversion: ConversionData = [
{
@ -63,6 +64,14 @@ const defaultConversion: ConversionData = [
return p;
},
},
{
from: (name) => {
return [CURSOR, ANCHOR, FOCUS].includes(name);
},
to: (name) => {
return { node: $(`<${name} />`), replace: true };
},
},
];
export default defaultConversion;

View File

@ -5,10 +5,10 @@ export const FOCUS = 'focus';
export const CURSOR = 'cursor';
export const ANCHOR_SELECTOR = 'span['
.concat(DATA_ELEMENT, '=')
.concat(ANCHOR, ']');
.concat(ANCHOR, '],anchor');
export const FOCUS_SELECTOR = 'span['
.concat(DATA_ELEMENT, '=')
.concat(FOCUS, ']');
.concat(FOCUS, '],focus');
export const CURSOR_SELECTOR = 'span['
.concat(DATA_ELEMENT, '=')
.concat(CURSOR, ']');
.concat(CURSOR, '],cursor');

View File

@ -134,7 +134,7 @@ class Engine<T extends EngineOptions = EngineOptions>
}
getValue(ignoreCursor: boolean = false) {
const value = this.change.getValue({});
const value = this.change.getValue({ ignoreCursor });
return ignoreCursor ? Selection.removeTags(value) : value;
}

View File

@ -44,6 +44,7 @@ class Conversion implements ConversionInterface {
let name = node.name;
let attributes = node.attributes();
let style = getStyleMap(attributes.style || '');
let replace = false;
//删除属性中的style属性
delete attributes.style;
// 光标相关节点
@ -103,18 +104,29 @@ class Conversion implements ConversionInterface {
style = {};
attributes = {};
} else {
const node =
const result =
typeof to === 'function'
? to(name, style, attributes)
: to;
name = node.name;
style = node.css();
attributes = node.attributes();
let resultNode: NodeInterface = result as NodeInterface;
if (result.hasOwnProperty('replace')) {
const newResult = result as {
node: NodeInterface;
replace: boolean;
};
resultNode = newResult.node;
replace = newResult.replace;
}
name = resultNode.name;
style = resultNode.css();
attributes = resultNode.attributes();
}
}
return result;
});
return rule ? { rule, node: { name, style, attributes } } : undefined;
return rule
? { rule, node: { name, style, attributes }, replace }
: undefined;
}
}
export default Conversion;

View File

@ -9,7 +9,13 @@ import {
ConversionRule,
SchemaRule,
} from '../types';
import { CARD_ELEMENT_KEY, CARD_SELECTOR } from '../constants';
import {
ANCHOR_SELECTOR,
CARD_ELEMENT_KEY,
CARD_SELECTOR,
CURSOR_SELECTOR,
FOCUS_SELECTOR,
} from '../constants';
import {
escape,
unescape,
@ -127,17 +133,19 @@ class Parser implements ParserInterface {
//把旧节点的子节点追加到新节点下
newNode.append(node.children());
if (node.isCard()) {
node.before(newNode);
node.remove();
node.replaceWith(newNode);
return newNode;
} else {
if (!nodeApi.isBlock(newNode, schema)) {
//把包含旧子节点的新节点追加到旧节点下
node.append(newNode);
if (value.replace) {
node.replaceWith(newNode);
} else {
//把包含旧子节点的新节点追加到旧节点下
node.append(newNode);
}
} else {
// 替换
node.before(newNode);
node.remove();
node.replaceWith(newNode);
//排除之前的过滤规则后再次过滤
value = conversion.transform(
newNode,
@ -168,6 +176,14 @@ class Parser implements ParserInterface {
if (!cardNode) return;
this.convert(conversion, cardNode, schema);
});
const cursors = root.find(
`${CURSOR_SELECTOR},${ANCHOR_SELECTOR},${FOCUS_SELECTOR}`,
);
cursors.each((_, index) => {
const cursor = cursors.eq(index);
if (!cursor) return;
this.convert(conversion, cursor, schema);
});
return;
}
// 过滤分割

View File

@ -26,7 +26,7 @@ export type ConversionToValue =
name: string,
style: { [key: string]: string },
attributes: { [key: string]: string },
) => NodeInterface);
) => NodeInterface | { node: NodeInterface; replace: boolean });
/**
*
*/
@ -78,6 +78,7 @@ export interface ConversionInterface {
[k: string]: string;
};
};
replace: boolean;
}
| undefined;
}