Fix overwrite surround (#2247)

* Revert "Revert to setFormat() to fix color setting"

This reverts commit 613b5c1034.

* Fix overwrite-surround
This commit is contained in:
Henrik Giesel 2022-12-09 01:10:58 +01:00 committed by GitHub
parent 3413115be7
commit 6d0a242a1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 17 deletions

View file

@ -9,7 +9,7 @@ import { boolMatcher } from "./match-type";
import { splitPartiallySelected } from "./split-text";
import type { SurroundFormat } from "./surround-format";
function surroundInner<T>(
function buildAndApply<T>(
node: Node,
buildFormat: BuildFormat<T>,
applyFormat: ApplyFormat<T>,
@ -19,30 +19,24 @@ function surroundInner<T>(
return buildFormat.recreateRange();
}
function reformatInner<T>(
function surroundOnCorrectNode<T>(
range: Range,
base: Element,
build: BuildFormat<T>,
apply: ApplyFormat<T>,
matcher: Matcher,
): Range {
const farthestMatchingAncestor = findFarthest(
const node = findFarthest(
range.commonAncestorContainer,
base,
matcher,
);
) ?? range.commonAncestorContainer;
if (farthestMatchingAncestor) {
return surroundInner(farthestMatchingAncestor, build, apply);
} else {
return surroundInner(range.commonAncestorContainer, build, apply);
}
return buildAndApply(node, build, apply);
}
/**
* Assumes that there are no matching ancestor elements above
* `range.commonAncestorContainer`. Make sure that the range is not placed
* inside the format before using this.
* Will surround the entire range, removing any contained formatting nodes in the process.
*/
export function surround<T>(
range: Range,
@ -52,7 +46,7 @@ export function surround<T>(
const splitRange = splitPartiallySelected(range);
const build = new BuildFormat(format, base, range, splitRange);
const apply = new ApplyFormat(format);
return surroundInner(range.commonAncestorContainer, build, apply);
return surroundOnCorrectNode(range, base, build, apply, boolMatcher(format));
}
/**
@ -66,7 +60,7 @@ export function reformat<T>(
const splitRange = splitPartiallySelected(range);
const build = new ReformatBuildFormat(format, base, range, splitRange);
const apply = new ReformatApplyFormat(format);
return reformatInner(range, base, build, apply, boolMatcher(format));
return surroundOnCorrectNode(range, base, build, apply, boolMatcher(format));
}
export function unsurround<T>(
@ -77,5 +71,5 @@ export function unsurround<T>(
const splitRange = splitPartiallySelected(range);
const build = new UnsurroundBuildFormat(format, base, range, splitRange);
const apply = new UnsurroundApplyFormat(format);
return reformatInner(range, base, build, apply, boolMatcher(format));
return surroundOnCorrectNode(range, base, build, apply, boolMatcher(format));
}

View file

@ -8,7 +8,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { getPlatformString } from "@tslib/shortcuts";
import { removeStyleProperties } from "@tslib/styling";
import { singleCallback } from "@tslib/typing";
import { setFormat } from "editor/old-editor-adapter";
import { onMount } from "svelte";
import IconButton from "../../components/IconButton.svelte";
@ -110,7 +109,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
removeFormats.update((formats) => [...formats, namedFormat]);
function setTextColor(): void {
setFormat("foreColor", transformedColor);
surrounder.overwriteSurround(key);
}
const setCombination = "F7";