Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pref: add folding function to code blocks #6177

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { NodeViewContent, NodeViewWrapper } from "@/tiptap/vue-3";
import { useTimeout } from "@vueuse/core";
import { computed } from "vue";
import BxBxsCopy from "~icons/bx/bxs-copy";
import RiArrowDownSFill from "~icons/ri/arrow-down-s-fill";
import RiArrowRightSFill from "~icons/ri/arrow-right-s-fill";
import IconCheckboxCircle from "~icons/ri/checkbox-circle-line";
import lowlight from "./lowlight";

Expand Down Expand Up @@ -33,6 +35,15 @@ const selectedLanguage = computed({
},
});

const collapsed = computed<boolean>({
get: () => {
return props.node.attrs.collapsed || false;
},
set: (collapsed: boolean) => {
props.updateAttributes({ collapsed: collapsed });
},
});

const { ready, start } = useTimeout(2000, { controls: true, immediate: false });

const handleCopyCode = () => {
Expand All @@ -44,14 +55,29 @@ const handleCopyCode = () => {
};
</script>
<template>
<node-view-wrapper as="div" class="code-node border-[1px] rounded mt-3">
<node-view-wrapper
as="div"
class="code-node border-[1px] rounded mt-3 overflow-hidden"
>
<div
class="bg-neutral-100 border-b-[1px] border-b-gray-100 py-1 flex items-center justify-between rounded-t"
contenteditable="false"
class="bg-neutral-100 border-b-[1px] border-b-gray-100 py-1 flex items-center justify-between"
>
<div class="flex-1 flex items-center pl-3">
<div
class="flex-1 flex items-center pl-3"
@click.self="collapsed ? (collapsed = false) : null"
>
<div class="pr-3 flex items-center">
<div
class="w-8 h-8 cursor-pointer rounded flex items-center justify-center hover:bg-zinc-200"
@click.stop="collapsed = !collapsed"
>
<RiArrowRightSFill v-if="collapsed" />
<RiArrowDownSFill v-else />
</div>
</div>
<select
v-model="selectedLanguage"
contenteditable="false"
class="block !leading-8 text-sm text-gray-900 border select-none border-transparent rounded-md bg-transparent focus:ring-blue-500 focus:border-blue-500 cursor-pointer hover:bg-zinc-200"
>
<option :value="null">auto</option>
Expand Down Expand Up @@ -80,6 +106,6 @@ const handleCopyCode = () => {
</div>
</div>
</div>
<pre><node-view-content as="code" class="hljs" /></pre>
<pre v-show="!collapsed"><node-view-content as="code" class="hljs" /></pre>
</node-view-wrapper>
</template>
19 changes: 19 additions & 0 deletions ui/packages/editor/src/extensions/code-block/code-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ export default CodeBlockLowlight.extend<
// otherwise the Mod-a shortcut key will be overridden.
priority: 110,
fakeSelection: true,

addAttributes() {
return {
...this.parent?.(),
collapsed: {
default: false,
parseHTML: (element) => !!element.getAttribute("collapsed"),
renderHTML: (attributes) => {
if (attributes.collapsed) {
return {
collapsed: attributes.collapsed,
};
}
return {};
},
},
};
},

addCommands() {
return {
...this.parent?.(),
Expand Down