fix: 解析样式的缓存会被修改

This commit is contained in:
yanmao 2022-01-15 15:03:05 +08:00
parent ed16059884
commit 635aafe156
13 changed files with 47 additions and 29 deletions

View File

@ -1,11 +1,13 @@
import { isHotkey } from 'is-hotkey';
import {
CARD_LEFT_SELECTOR,
CARD_LOADING_KEY,
CARD_RIGHT_SELECTOR,
CARD_SELECTOR,
DATA_ELEMENT,
DATA_ID,
EDITABLE,
READY_CARD_KEY,
READY_CARD_SELECTOR,
ROOT,
ROOT_SELECTOR,
@ -566,7 +568,13 @@ class NativeEvent {
`${CARD_SELECTOR},${READY_CARD_SELECTOR}`,
);
if (card.length > 0) {
range.setStartAfter(card);
const attributes = card.attributes();
if (
attributes[CARD_LOADING_KEY] ||
attributes[READY_CARD_KEY]
) {
range.setStartAfter(card);
}
}
const selection = range.createSelection();
this.engine.card.render(undefined, (count) => {

View File

@ -28,10 +28,11 @@ abstract class InlineEntry<T extends PluginOptions = PluginOptions>
super.init();
const editor = this.editor;
if (isEngine(editor) && this.markdown) {
editor.on(
'paste:markdown-check',
(child) => !this.checkMarkdown(child)?.match,
);
// inline 样式的粘贴的时候不检测,以免误报
// editor.on(
// 'paste:markdown-check',
// (child) => !this.checkMarkdown(child)?.match,
// );
editor.on('paste:markdown', (child) => this.pasteMarkdown(child));
}
}

View File

@ -54,10 +54,11 @@ abstract class MarkEntry<T extends PluginOptions = PluginOptions>
super.init();
const editor = this.editor;
if (isEngine(editor) && this.markdown) {
editor.on(
'paste:markdown-check',
(child) => !this.checkMarkdown(child)?.match,
);
// mark 样式的粘贴的时候不检测,以免误报
// editor.on(
// 'paste:markdown-check',
// (child) => !this.checkMarkdown(child)?.match,
// );
editor.on('paste:markdown', (node) => this.pasteMarkdown(node));
}
}

View File

@ -7,6 +7,7 @@ import {
} from '../constants/card';
import { DATA_ELEMENT } from '../constants/root';
import { isMacos } from './user-agent';
import md5 from 'blueimp-md5';
/**
*
@ -95,30 +96,30 @@ export const getAttrMap = (value: string): { [k: string]: string } => {
return map;
};
const stylesCaches = new Map<string, { [k: string]: string }>();
const stylesCaches: Record<string, Record<string, string>> = {};
/**
* style map
* @param {string} style
*/
export const getStyleMap = (style: string): { [k: string]: string } => {
style = style.replace(/&quot;/g, '"');
const map: { [k: string]: string } = {};
if (!style) return map;
const cacheStyle = stylesCaches.get(style);
if (cacheStyle) return cacheStyle;
export const getStyleMap = (style: string): Record<string, string> => {
const key = md5(style);
const map: Record<string, string> = {};
if (!key) return { ...map };
const cacheStyle = stylesCaches[key];
if (cacheStyle) return { ...cacheStyle };
const reg = /\s*([\w\-]+)\s*:([^;]*)(;|$)/g;
let match;
while ((match = reg.exec(style))) {
const key = match[1].toLowerCase().trim();
let val = match[2].trim();
if (val.toLowerCase().includes('rgb')) {
if (key.endsWith('color') || val.toLowerCase().includes('rgb')) {
val = toHex(val);
}
map[key] = val;
}
stylesCaches.set(style, map);
return map;
stylesCaches[key] = map;
return { ...map };
};
/**

View File

@ -725,7 +725,7 @@ export const getToolbarDefaultConfig = (
return (
(!!cardComponent &&
!cardComponent.isCursor(range.startNode)) ||
range.commonAncestorNode.find(CARD_SELECTOR).length > 0 ||
range.containsCard() ||
!engine.command.queryEnabled('link')
);
},

View File

@ -30,6 +30,7 @@ export default class Popup {
window.addEventListener('resize', this.onSelect);
this.#editor.scrollNode?.on('scroll', this.onSelect);
document.addEventListener('mousedown', this.hide);
this.#editor.on('blur', this.hide);
}
onSelect = () => {
@ -173,6 +174,7 @@ export default class Popup {
window.removeEventListener('resize', this.onSelect);
this.#editor.scrollNode?.off('scroll', this.onSelect);
document.removeEventListener('mousedown', this.hide);
this.#editor.off('blur', this.hide);
if (this.#vm) this.#vm.unmount();
}
}

View File

@ -879,7 +879,7 @@ export const getToolbarDefaultConfig = (
return (
(!!cardComponent &&
!cardComponent.isCursor(range.startNode)) ||
range.commonAncestorNode.find(CARD_SELECTOR).length > 0 ||
range.containsCard() ||
!engine.command.queryEnabled('link')
);
},

View File

@ -30,6 +30,7 @@ export default class Popup {
window.addEventListener('resize', this.onSelect);
this.#editor.scrollNode?.on('scroll', this.onSelect);
document.addEventListener('mousedown', this.hide);
this.#editor.on('blur', this.hide);
}
onSelect = () => {
@ -179,6 +180,7 @@ export default class Popup {
window.removeEventListener('resize', this.onSelect);
this.#editor.scrollNode?.off('scroll', this.onSelect);
document.removeEventListener('mousedown', this.hide);
this.#editor.off('blur', this.hide);
}
}
export type { GroupItemProps };

View File

@ -80,9 +80,9 @@ export default defineComponent({
}
onMounted(() => {
if (props.onLoad) props.onLoad();
linkRef.value?.focus();
setTimeout(() => {
linkRef.value?.focus();
if (props.onLoad) props.onLoad();
}, 200);
})

View File

@ -134,6 +134,7 @@ class Toolbar {
onLoad: () => {
this.mouseInContainer = true;
if (callback) callback();
this.engine.trigger('select');
},
onOk: (text: string, link: string) => this.onOk(text, link),
});

View File

@ -29,11 +29,9 @@ const LinkEditor: React.FC<LinkEditorProps> = ({
const linkRef = useRef<Input>(null);
useEffect(() => {
linkRef.current?.focus();
if (onLoad && linkRef.current) onLoad(linkRef.current);
setTimeout(() => {
linkRef.current?.focus();
}, 200);
}, []);
}, [linkRef]);
return (
<div

View File

@ -134,7 +134,10 @@ class Toolbar {
language={this.engine.language}
defaultText={text}
defaultLink={href}
onLoad={() => (this.mouseInContainer = true)}
onLoad={() => {
this.mouseInContainer = true;
this.engine.trigger('select');
}}
onOk={(text: string, link: string) => this.onOk(text, link)}
/>
);

View File

@ -12,6 +12,7 @@ import {
decodeCardValue,
CARD_VALUE_KEY,
CARD_SELECTOR,
transformCustomTags,
} from '@aomao/engine';
import TableComponent, { Template, Helper } from './component';
import locales from './locale';
@ -476,7 +477,7 @@ class Table<T extends TableOptions = TableOptions> extends Plugin<T> {
);
if (!value || !value.html) return;
// 表格值里面的卡片都是没有被转换过的,所以需要先把卡片转换过来
table = $(value.html);
table = $(transformCustomTags(value.html));
if (table.length === 0) {
node.remove();
return;