2022-11-18 15:04:37 +00:00
|
|
|
// Forked from https://codeberg.org/retronav/ixora
|
|
|
|
// Original author: Pranav Karawale
|
|
|
|
// License: Apache License 2.0.
|
|
|
|
|
2022-12-09 15:09:53 +00:00
|
|
|
import { Decoration, EditorState, syntaxTree } from "../deps.ts";
|
2022-11-18 15:04:37 +00:00
|
|
|
import {
|
|
|
|
checkRangeOverlap,
|
2022-12-09 15:09:53 +00:00
|
|
|
decoratorStateField,
|
2022-11-18 15:04:37 +00:00
|
|
|
invisibleDecoration,
|
|
|
|
isCursorInRange,
|
|
|
|
} from "./util.ts";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These types contain markers as child elements that can be hidden.
|
|
|
|
*/
|
|
|
|
const typesWithMarks = [
|
|
|
|
"Emphasis",
|
|
|
|
"StrongEmphasis",
|
|
|
|
"InlineCode",
|
|
|
|
"Highlight",
|
|
|
|
"Strikethrough",
|
|
|
|
];
|
|
|
|
/**
|
|
|
|
* The elements which are used as marks.
|
|
|
|
*/
|
|
|
|
const markTypes = [
|
|
|
|
"EmphasisMark",
|
|
|
|
"CodeMark",
|
|
|
|
"HighlightMark",
|
|
|
|
"StrikethroughMark",
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2022-12-09 15:09:53 +00:00
|
|
|
* Ixora hide marks plugin.
|
|
|
|
*
|
|
|
|
* This plugin allows to:
|
|
|
|
* - Hide marks when they are not in the editor selection.
|
2022-11-18 15:04:37 +00:00
|
|
|
*/
|
2022-12-09 15:09:53 +00:00
|
|
|
export function hideMarksPlugin() {
|
|
|
|
return decoratorStateField((state: EditorState) => {
|
2022-11-18 15:04:37 +00:00
|
|
|
const widgets: any[] = [];
|
|
|
|
let parentRange: [number, number];
|
2022-12-09 15:09:53 +00:00
|
|
|
syntaxTree(state).iterate({
|
2022-11-18 15:04:37 +00:00
|
|
|
enter: ({ type, from, to, node }) => {
|
|
|
|
if (typesWithMarks.includes(type.name)) {
|
|
|
|
// There can be a possibility that the current node is a
|
|
|
|
// child eg. a bold node in a emphasis node, so check
|
|
|
|
// for that or else save the node range
|
|
|
|
if (
|
|
|
|
parentRange &&
|
|
|
|
checkRangeOverlap([from, to], parentRange)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
} else parentRange = [from, to];
|
2022-12-09 15:09:53 +00:00
|
|
|
if (isCursorInRange(state, [from, to])) return;
|
2022-11-18 15:04:37 +00:00
|
|
|
const innerTree = node.toTree();
|
|
|
|
innerTree.iterate({
|
|
|
|
enter({ type, from: markFrom, to: markTo }) {
|
|
|
|
// Check for mark types and push the replace
|
|
|
|
// decoration
|
|
|
|
if (!markTypes.includes(type.name)) return;
|
|
|
|
widgets.push(
|
|
|
|
invisibleDecoration.range(
|
|
|
|
from + markFrom,
|
|
|
|
from + markTo,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return Decoration.set(widgets, true);
|
2022-12-09 15:09:53 +00:00
|
|
|
});
|
2022-11-18 15:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HEADINGS
|
|
|
|
|
2022-12-09 15:09:53 +00:00
|
|
|
export function hideHeaderMarkPlugin() {
|
|
|
|
return decoratorStateField((state) => {
|
2022-11-18 15:04:37 +00:00
|
|
|
const widgets: any[] = [];
|
2022-12-09 15:09:53 +00:00
|
|
|
syntaxTree(state).iterate({
|
2022-11-18 15:04:37 +00:00
|
|
|
enter: ({ type, from, to }) => {
|
2022-12-09 15:09:53 +00:00
|
|
|
if (!type.name.startsWith("ATXHeading")) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
// Get the active line
|
2022-12-09 15:09:53 +00:00
|
|
|
const line = state.sliceDoc(from, to);
|
|
|
|
if (isCursorInRange(state, [from, to])) {
|
2022-11-18 15:04:37 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.line({ class: "sb-header-inside" }).range(from),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2022-12-09 15:09:53 +00:00
|
|
|
|
2022-12-27 14:22:14 +00:00
|
|
|
const spacePos = line.indexOf(" ");
|
|
|
|
if (spacePos === -1) {
|
|
|
|
// Not complete header
|
|
|
|
return;
|
|
|
|
}
|
2022-12-09 15:09:53 +00:00
|
|
|
widgets.push(
|
|
|
|
invisibleDecoration.range(
|
|
|
|
from,
|
2022-12-27 14:22:14 +00:00
|
|
|
from + spacePos + 1,
|
2022-12-09 15:09:53 +00:00
|
|
|
),
|
|
|
|
);
|
2022-11-18 15:04:37 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return Decoration.set(widgets, true);
|
2022-12-09 15:09:53 +00:00
|
|
|
});
|
2022-11-18 15:04:37 +00:00
|
|
|
}
|