현재 선택된 부분에 따라 데코를 유지할건지 없앨것인지 결정
This commit is contained in:
parent
cb915eb4d0
commit
3370b5bd6f
|
@ -27,12 +27,20 @@ class StatefulDecorationSet {
|
||||||
}
|
}
|
||||||
|
|
||||||
async computeAsyncDecorations(tokens: TokenSpec[]): Promise<DecorationSet | null> {
|
async computeAsyncDecorations(tokens: TokenSpec[]): Promise<DecorationSet | null> {
|
||||||
|
// 현재 모드 판별
|
||||||
const isSourceMode = !this.editor.state.field(editorLivePreviewField);
|
const isSourceMode = !this.editor.state.field(editorLivePreviewField);
|
||||||
if (isSourceMode) {
|
// 현재 선택된 부분
|
||||||
return Decoration.none;
|
const selectFrom = this.editor.state.selection.main.from;
|
||||||
} else {
|
const selectTo = this.editor.state.selection.main.to;
|
||||||
|
|
||||||
const decorations: Range<Decoration>[] = [];
|
const decorations: Range<Decoration>[] = [];
|
||||||
for (const token of tokens) {
|
for (const token of tokens) {
|
||||||
|
const isSelected = (selectFrom <= token.to && selectTo >= token.from) || (selectFrom >= token.from && selectTo <= token.to);
|
||||||
|
|
||||||
|
if (isSelected || isSourceMode) {
|
||||||
|
return Decoration.none;
|
||||||
|
}
|
||||||
|
|
||||||
let deco = this.decoCache[token.value];
|
let deco = this.decoCache[token.value];
|
||||||
if (!deco) {
|
if (!deco) {
|
||||||
const div = createDiv();
|
const div = createDiv();
|
||||||
|
@ -44,20 +52,18 @@ class StatefulDecorationSet {
|
||||||
linkEl.href = token.value;
|
linkEl.href = token.value;
|
||||||
linkEl.addClass("markdown-rendered");
|
linkEl.addClass("markdown-rendered");
|
||||||
div.appendChild(linkEl);
|
div.appendChild(linkEl);
|
||||||
const params = await LinkThumbnailWidgetParams(token.value);
|
LinkThumbnailWidgetParams(token.value).then(params => {
|
||||||
if (params != null) {
|
if (params) {
|
||||||
linkEl.innerHTML = params;
|
linkEl.innerHTML = params;
|
||||||
linkEl.addEventListener("click", (e) => e.stopPropagation());
|
linkEl.addEventListener("click", (e) => e.stopPropagation());
|
||||||
} else {
|
|
||||||
return Decoration.none;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
deco = this.decoCache[token.value] = Decoration.replace({widget: new EmojiWidget(div), block: true});
|
deco = this.decoCache[token.value] = Decoration.replace({widget: new EmojiWidget(div), block: true});
|
||||||
}
|
}
|
||||||
decorations.push(deco.range(token.from, token.to));
|
decorations.push(deco.range(token.from, token.to));
|
||||||
}
|
}
|
||||||
return Decoration.set(decorations, true);
|
return Decoration.set(decorations, true);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
debouncedUpdate = debounce(this.updateAsyncDecorations, 100, true);
|
debouncedUpdate = debounce(this.updateAsyncDecorations, 100, true);
|
||||||
|
|
||||||
|
@ -81,7 +87,7 @@ function buildViewPlugin(plugin: LinkThumbnailPlugin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
update(update: ViewUpdate) {
|
update(update: ViewUpdate) {
|
||||||
if (update.docChanged || update.viewportChanged) {
|
if (update.docChanged || update.viewportChanged || update.selectionSet) {
|
||||||
this.buildAsyncDecorations(update.view);
|
this.buildAsyncDecorations(update.view);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,9 +99,9 @@ function buildViewPlugin(plugin: LinkThumbnailPlugin) {
|
||||||
tree.iterate({
|
tree.iterate({
|
||||||
enter: ({node, from, to}) => {
|
enter: ({node, from, to}) => {
|
||||||
const tokenProps = node.type.prop<string>(tokenClassNodeProp);
|
const tokenProps = node.type.prop<string>(tokenClassNodeProp);
|
||||||
|
|
||||||
if (tokenProps && node.name === "url") {
|
if (tokenProps && node.name === "url") {
|
||||||
const value = view.state.doc.sliceString(from, to);
|
const value = view.state.doc.sliceString(from, to);
|
||||||
console.log(tokenProps, node, value, view.state);
|
|
||||||
if (value) {
|
if (value) {
|
||||||
targetElements.push({from: from, to: to, value: value});
|
targetElements.push({from: from, to: to, value: value});
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ export class PostProcessor {
|
||||||
context: MarkdownPostProcessorContext
|
context: MarkdownPostProcessorContext
|
||||||
) => {
|
) => {
|
||||||
// 링크 변환
|
// 링크 변환
|
||||||
const linkEls:Element[] = element.findAll("a.external-link:not(.cm-formatting)");
|
const linkEls:Element[] = element.findAll("a.external-link:not(.cm-formatting, .markdown-rendered)");
|
||||||
for (const linkEl of linkEls) {
|
for (const linkEl of linkEls) {
|
||||||
// dataview 클래스를 가진 부모 요소를 확인합니다.
|
// dataview 클래스를 가진 부모 요소를 확인합니다.
|
||||||
if (linkEl.closest(".dataview") !== null) {
|
if (linkEl.closest(".dataview") !== null) {
|
||||||
|
@ -24,7 +24,6 @@ export class PostProcessor {
|
||||||
const params = await LinkThumbnailWidgetParams(url);
|
const params = await LinkThumbnailWidgetParams(url);
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
linkEl.innerHTML = params;
|
linkEl.innerHTML = params;
|
||||||
linkEl.removeClass("external-link");
|
|
||||||
linkEl.addClass("markdown-rendered");
|
linkEl.addClass("markdown-rendered");
|
||||||
linkEl.addEventListener("click", (e) => e.stopPropagation());
|
linkEl.addEventListener("click", (e) => e.stopPropagation());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue