mirror of
https://github.com/docmost/docmost.git
synced 2026-05-11 08:54:06 +08:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 52b34bc6f4 |
@@ -20,7 +20,6 @@ import {
|
||||
IconCalendar,
|
||||
IconAppWindow,
|
||||
IconSitemap,
|
||||
IconLayoutColumns,
|
||||
} from "@tabler/icons-react";
|
||||
import {
|
||||
CommandProps,
|
||||
@@ -244,51 +243,6 @@ const CommandGroups: SlashMenuGroupedItemsType = {
|
||||
.insertTable({ rows: 3, cols: 3, withHeaderRow: true })
|
||||
.run(),
|
||||
},
|
||||
{
|
||||
title: "Columns",
|
||||
description: "Insert 2 columns layout.",
|
||||
searchTerms: ["columns", "layout", "grid", "side by side"],
|
||||
icon: IconLayoutColumns,
|
||||
command: ({ editor, range }: CommandProps) => {
|
||||
editor
|
||||
.chain()
|
||||
.focus()
|
||||
.deleteRange(range)
|
||||
.insertContent({
|
||||
type: "column_container",
|
||||
content: [
|
||||
{
|
||||
type: "column",
|
||||
attrs: { colWidth: 200 },
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "column",
|
||||
attrs: { colWidth: 200 },
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "column",
|
||||
attrs: { colWidth: 200 },
|
||||
content: [
|
||||
{
|
||||
type: "paragraph",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
.run();
|
||||
},
|
||||
},
|
||||
{
|
||||
title: "Toggle block",
|
||||
description: "Insert collapsible block.",
|
||||
|
||||
@@ -46,7 +46,6 @@ import {
|
||||
Heading,
|
||||
Highlight,
|
||||
UniqueID,
|
||||
ColumnsExtension,
|
||||
} from "@docmost/editor-ext";
|
||||
import {
|
||||
randomElement,
|
||||
@@ -230,7 +229,6 @@ export const mainExtensions = [
|
||||
Subpages.configure({
|
||||
view: SubpagesView,
|
||||
}),
|
||||
ColumnsExtension,
|
||||
MarkdownClipboard.configure({
|
||||
transformPastedText: true,
|
||||
}),
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
.resize-cursor {
|
||||
cursor: col-resize;
|
||||
}
|
||||
|
||||
.prosemirror-column-container {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
width: calc(100% - 8px);
|
||||
gap: 12px;
|
||||
margin: 16px 0;
|
||||
}
|
||||
|
||||
.prosemirror-column-container.has-focus .prosemirror-column,
|
||||
.prosemirror-column-container:hover .prosemirror-column {
|
||||
background-color: rgba(100, 106, 115, 0.05);
|
||||
}
|
||||
|
||||
.prosemirror-column-container .prosemirror-column {
|
||||
position: relative;
|
||||
border-radius: 8px;
|
||||
min-width: 50px;
|
||||
padding: 12px;
|
||||
background-color: transparent;
|
||||
transition: background-color 0.2s ease;
|
||||
}
|
||||
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
> :not(div.grid-resize-handle):nth-child(1),
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
> div.grid-resize-handle
|
||||
+ :nth-child(2) {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.prosemirror-column-container .prosemirror-column > :nth-last-child(1) {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.prosemirror-column-container .prosemirror-column .grid-resize-handle {
|
||||
position: absolute;
|
||||
right: -7px;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
width: 2px;
|
||||
z-index: 20;
|
||||
background-color: #336df4;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
.grid-resize-handle
|
||||
.circle-button {
|
||||
top: -8px;
|
||||
left: -9px;
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
background-color: #007bff;
|
||||
border: 4px solid white;
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
position: relative;
|
||||
pointer-events: auto;
|
||||
cursor: pointer;
|
||||
transition: transform 0.1s ease-in-out;
|
||||
}
|
||||
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
.grid-resize-handle
|
||||
.circle-button:hover {
|
||||
transform: scale(1.35);
|
||||
}
|
||||
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
.grid-resize-handle
|
||||
.circle-button
|
||||
.plus {
|
||||
position: relative;
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
}
|
||||
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
.grid-resize-handle
|
||||
.circle-button
|
||||
.plus::before,
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
.grid-resize-handle
|
||||
.circle-button
|
||||
.plus::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
background-color: white;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
}
|
||||
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
.grid-resize-handle
|
||||
.circle-button
|
||||
.plus::before {
|
||||
width: 8px;
|
||||
height: 2px;
|
||||
}
|
||||
|
||||
.prosemirror-column-container
|
||||
.prosemirror-column
|
||||
.grid-resize-handle
|
||||
.circle-button
|
||||
.plus::after {
|
||||
width: 24px;
|
||||
height: 8px;
|
||||
}
|
||||
@@ -13,4 +13,3 @@
|
||||
@import "./mention.css";
|
||||
@import "./ordered-list.css";
|
||||
@import "./highlight.css";
|
||||
@import "./column.css";
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
"@nestjs-labs/nestjs-ioredis": "^11.0.4",
|
||||
"@nestjs/bullmq": "^11.0.4",
|
||||
"@nestjs/common": "^11.1.9",
|
||||
"nestjs-cls": "^4.5.0",
|
||||
"@nestjs/config": "^4.0.2",
|
||||
"@nestjs/core": "^11.1.9",
|
||||
"@nestjs/event-emitter": "^3.0.1",
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { APP_INTERCEPTOR } from '@nestjs/core';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { AuditActorInterceptor } from './common/interceptors/audit-actor.interceptor';
|
||||
import { CoreModule } from './core/core.module';
|
||||
import { EnvironmentModule } from './integrations/environment/environment.module';
|
||||
import { CollaborationModule } from './collaboration/collaboration.module';
|
||||
@@ -18,6 +20,7 @@ import { SecurityModule } from './integrations/security/security.module';
|
||||
import { TelemetryModule } from './integrations/telemetry/telemetry.module';
|
||||
import { RedisModule } from '@nestjs-labs/nestjs-ioredis';
|
||||
import { RedisConfigService } from './integrations/redis/redis-config.service';
|
||||
import { ClsModule } from 'nestjs-cls';
|
||||
|
||||
const enterpriseModules = [];
|
||||
try {
|
||||
@@ -35,6 +38,10 @@ try {
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ClsModule.forRoot({
|
||||
global: true,
|
||||
middleware: { mount: true },
|
||||
}),
|
||||
CoreModule,
|
||||
DatabaseModule,
|
||||
EnvironmentModule,
|
||||
@@ -60,6 +67,12 @@ try {
|
||||
...enterpriseModules,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
providers: [
|
||||
AppService,
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: AuditActorInterceptor,
|
||||
},
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -35,7 +35,6 @@ import {
|
||||
Subpages,
|
||||
Highlight,
|
||||
UniqueID,
|
||||
ColumnsExtension,
|
||||
addUniqueIdsToDoc,
|
||||
} from '@docmost/editor-ext';
|
||||
import { generateText, getSchema, JSONContent } from '@tiptap/core';
|
||||
@@ -89,7 +88,6 @@ export const tiptapExtensions = [
|
||||
Embed,
|
||||
Mention,
|
||||
Subpages,
|
||||
ColumnsExtension
|
||||
] as any;
|
||||
|
||||
export function jsonToHtml(tiptapJson: any) {
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
export const AuditEvent = {
|
||||
// Workspace Invitations
|
||||
WORKSPACE_CREATED: 'workspace.created',
|
||||
WORKSPACE_INVITE_CREATED: 'workspace.invite_created',
|
||||
WORKSPACE_INVITE_REVOKED: 'workspace.invite_revoked',
|
||||
|
||||
WORKSPACE_INVITE_ACCEPTED: 'workspace.invite_accepted',
|
||||
|
||||
WORKSPACE_USER_CREATED: 'workspace.user_created',
|
||||
WORKSPACE_USER_DEACTIVATED: 'workspace.user_deactivated',
|
||||
|
||||
WORKSPACE_ALLOWED_DOMAIN_UPDATED: 'workspace.allowed_domain_updated',
|
||||
WORKSPACE_ICON_CHANGED: 'workspace.icon_changed',
|
||||
WORKSPACE_NAME_CHANGED: 'workspace.name_changed',
|
||||
|
||||
WORKSPACE_AI_TOGGLED: 'workspace.ai_toggled',
|
||||
|
||||
USER_CREATED: 'user.created',
|
||||
USER_DELETED: 'user.deleted',
|
||||
USER_LOGIN: 'user.login',
|
||||
USER_LOGOUT: 'user.logout',
|
||||
USER_ROLE_CHANGED: 'user.user_role_changed',
|
||||
USER_PASSWORD_CHANGED: 'user.password_changed',
|
||||
USER_PASSWORD_RESET: 'user.reset_password',
|
||||
USER_PHOTO_CHANGED: 'user.reset_password',
|
||||
USER_NAME_CHANGED: 'user.name_changed',
|
||||
USER_EMAIL_CHANGED: 'user.email_changed',
|
||||
USER_MFA_SETUP: 'user.mfa_setup',
|
||||
USER_MFA_BACKUP_CODE_GENERATED: 'user.mfa_backup_code_generated',
|
||||
|
||||
// API Keys
|
||||
API_KEY_CREATED: 'api_key.created',
|
||||
API_KEY_UPDATED: 'api_key.updated',
|
||||
API_KEY_DELETED: 'api_key.deleted',
|
||||
|
||||
// Space
|
||||
SPACE_CREATED: 'space.created',
|
||||
SPACE_UPDATED: 'space.updated',
|
||||
SPACE_DELETED: 'space.deleted',
|
||||
|
||||
SPACE_MEMBER_ADDED: 'space.member_added',
|
||||
SPACE_MEMBER_REMOVED: 'space.member_removed',
|
||||
SPACE_MEMBER_ROLE_CHANGED: 'space.member_role_changed',
|
||||
|
||||
// OR SPACE_USER_ADDED: 'space.user_added',
|
||||
// SPACE_GROUP_ADDED: 'space.group_added',
|
||||
|
||||
// GROUP
|
||||
GROUP_CREATED: 'group.created',
|
||||
GROUP_UPDATED: 'group.updated',
|
||||
GROUP_DELETED: 'group.deleted',
|
||||
|
||||
GROUP_MEMBER_ADDED: 'group.member_added',
|
||||
GROUP_MEMBER_REMOVED: 'group.member_removed',
|
||||
|
||||
// Comments
|
||||
COMMENT_CREATED: 'comment.created',
|
||||
COMMENT_UPDATED: 'comment.updated',
|
||||
COMMENT_DELETED: 'comment.deleted',
|
||||
COMMENT_RESOLVED: 'comment.resolved',
|
||||
COMMENT_REOPENED: 'comment.reopened',
|
||||
|
||||
// PAGE
|
||||
PAGE_CREATED: 'page.created',
|
||||
PAGE_UPDATED: 'page.updated',
|
||||
PAGE_TRASHED: 'page.trash',
|
||||
PAGE_DELETED: 'page.deleted',
|
||||
PAGE_SHARED: 'page.shared',
|
||||
|
||||
ATTACHMENT_UPLOADED: 'attachment.uploaded',
|
||||
|
||||
PAGE_IMPORTED: 'page.imported',
|
||||
PAGE_RESTORED: 'page.restored',
|
||||
PAGE_EXPORTED: 'page.exported',
|
||||
SPACE_EXPORTED: 'space.imported',
|
||||
|
||||
// SSO EVENTS
|
||||
} as const;
|
||||
|
||||
export type AuditEventType = (typeof AuditEvent)[keyof typeof AuditEvent];
|
||||
|
||||
export type ActorType = 'user' | 'system' | 'api_key';
|
||||
|
||||
export interface AuditLogPayload {
|
||||
event: AuditEventType;
|
||||
resourceType: string;
|
||||
resourceId?: string;
|
||||
changes?: {
|
||||
before?: Record<string, any>;
|
||||
after?: Record<string, any>;
|
||||
};
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface AuditLogData extends AuditLogPayload {
|
||||
workspaceId: string;
|
||||
actorId?: string;
|
||||
actorType: ActorType;
|
||||
ipAddress?: string;
|
||||
userAgent?: string;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
import { ClsService } from 'nestjs-cls';
|
||||
import { AuditContext, AUDIT_CONTEXT_KEY } from '../middlewares/audit-context.middleware';
|
||||
|
||||
@Injectable()
|
||||
export class AuditActorInterceptor implements NestInterceptor {
|
||||
constructor(private readonly cls: ClsService) {}
|
||||
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const user = request.user?.user;
|
||||
|
||||
if (user?.id) {
|
||||
const auditContext = this.cls.get<AuditContext>(AUDIT_CONTEXT_KEY);
|
||||
if (auditContext) {
|
||||
auditContext.actorId = user.id;
|
||||
this.cls.set(AUDIT_CONTEXT_KEY, auditContext);
|
||||
}
|
||||
}
|
||||
|
||||
return next.handle();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { Injectable, NestMiddleware } from '@nestjs/common';
|
||||
import { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { ClsService } from 'nestjs-cls';
|
||||
|
||||
export interface AuditContext {
|
||||
workspaceId: string | null;
|
||||
actorId: string | null;
|
||||
actorType: 'user' | 'system' | 'api_key';
|
||||
ipAddress: string | null;
|
||||
}
|
||||
|
||||
export const AUDIT_CONTEXT_KEY = 'auditContext';
|
||||
|
||||
@Injectable()
|
||||
export class AuditContextMiddleware implements NestMiddleware {
|
||||
constructor(private readonly cls: ClsService) {}
|
||||
|
||||
use(req: FastifyRequest['raw'], res: FastifyReply['raw'], next: () => void) {
|
||||
const workspaceId = (req as any).workspaceId ?? null;
|
||||
const ipAddress = this.extractIpAddress(req);
|
||||
|
||||
const auditContext: AuditContext = {
|
||||
workspaceId,
|
||||
actorId: null,
|
||||
actorType: 'user',
|
||||
ipAddress,
|
||||
};
|
||||
|
||||
this.cls.set(AUDIT_CONTEXT_KEY, auditContext);
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
private extractIpAddress(req: FastifyRequest['raw']): string | null {
|
||||
const xForwardedFor = req.headers['x-forwarded-for'];
|
||||
if (xForwardedFor) {
|
||||
const ips = Array.isArray(xForwardedFor)
|
||||
? xForwardedFor[0]
|
||||
: xForwardedFor.split(',')[0];
|
||||
return ips?.trim() ?? null;
|
||||
}
|
||||
|
||||
const xRealIp = req.headers['x-real-ip'];
|
||||
if (xRealIp) {
|
||||
return Array.isArray(xRealIp) ? xRealIp[0] : xRealIp;
|
||||
}
|
||||
|
||||
return (req as any).socket?.remoteAddress ?? null;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,13 @@ import { SpaceModule } from './space/space.module';
|
||||
import { GroupModule } from './group/group.module';
|
||||
import { CaslModule } from './casl/casl.module';
|
||||
import { DomainMiddleware } from '../common/middlewares/domain.middleware';
|
||||
import { AuditContextMiddleware } from '../common/middlewares/audit-context.middleware';
|
||||
import { ShareModule } from './share/share.module';
|
||||
import {
|
||||
AUDIT_SERVICE,
|
||||
NoopAuditService,
|
||||
} from '../integrations/audit/audit.service';
|
||||
import { ClsMiddleware } from 'nestjs-cls';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -31,17 +37,31 @@ import { ShareModule } from './share/share.module';
|
||||
CaslModule,
|
||||
ShareModule,
|
||||
],
|
||||
providers: [
|
||||
{
|
||||
provide: AUDIT_SERVICE,
|
||||
useClass: NoopAuditService,
|
||||
},
|
||||
],
|
||||
exports: [AUDIT_SERVICE],
|
||||
})
|
||||
export class CoreModule implements NestModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
const excludedRoutes = [
|
||||
{ path: 'auth/setup', method: RequestMethod.POST },
|
||||
{ path: 'health', method: RequestMethod.GET },
|
||||
{ path: 'health/live', method: RequestMethod.GET },
|
||||
{ path: 'billing/stripe/webhook', method: RequestMethod.POST },
|
||||
];
|
||||
|
||||
consumer
|
||||
.apply(DomainMiddleware)
|
||||
.exclude(
|
||||
{ path: 'auth/setup', method: RequestMethod.POST },
|
||||
{ path: 'health', method: RequestMethod.GET },
|
||||
{ path: 'health/live', method: RequestMethod.GET },
|
||||
{ path: 'billing/stripe/webhook', method: RequestMethod.POST },
|
||||
)
|
||||
.exclude(...excludedRoutes)
|
||||
.forRoutes('*');
|
||||
|
||||
consumer
|
||||
.apply(AuditContextMiddleware)
|
||||
.exclude(...excludedRoutes)
|
||||
.forRoutes('*');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Inject,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
@@ -14,6 +15,11 @@ import { RemoveSpaceMemberDto } from '../dto/remove-space-member.dto';
|
||||
import { UpdateSpaceMemberRoleDto } from '../dto/update-space-member-role.dto';
|
||||
import { SpaceRole } from '../../../common/helpers/types/permission';
|
||||
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||
import { AuditEvent } from '../../../common/events/audit-events';
|
||||
import {
|
||||
AUDIT_SERVICE,
|
||||
IAuditService,
|
||||
} from '../../../integrations/audit/audit.service';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceMemberService {
|
||||
@@ -21,6 +27,7 @@ export class SpaceMemberService {
|
||||
private spaceMemberRepo: SpaceMemberRepo,
|
||||
private spaceRepo: SpaceRepo,
|
||||
@InjectKysely() private readonly db: KyselyDB,
|
||||
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
||||
) {}
|
||||
|
||||
async addUserToSpace(
|
||||
@@ -161,8 +168,43 @@ export class SpaceMemberService {
|
||||
|
||||
if (membersToAdd.length > 0) {
|
||||
await this.spaceMemberRepo.insertSpaceMember(membersToAdd);
|
||||
} else {
|
||||
// either they are already members or do not exist on the workspace
|
||||
|
||||
// Audit log for each member added
|
||||
for (const user of validUsers) {
|
||||
this.auditService.log({
|
||||
event: AuditEvent.SPACE_MEMBER_ADDED,
|
||||
resourceType: 'space_members',
|
||||
resourceId: dto.spaceId,
|
||||
changes: {
|
||||
after: { role: dto.role },
|
||||
},
|
||||
metadata: {
|
||||
spaceId: dto.spaceId,
|
||||
spaceName: space.name,
|
||||
userId: user.id,
|
||||
userName: user.name,
|
||||
memberType: 'user',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const group of validGroups) {
|
||||
this.auditService.log({
|
||||
event: AuditEvent.SPACE_MEMBER_ADDED,
|
||||
resourceType: 'space_members',
|
||||
resourceId: dto.spaceId,
|
||||
changes: {
|
||||
after: { role: dto.role },
|
||||
},
|
||||
metadata: {
|
||||
spaceId: dto.spaceId,
|
||||
spaceName: space.name,
|
||||
groupId: group.id,
|
||||
groupName: group.name,
|
||||
memberType: 'group',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,6 +251,22 @@ export class SpaceMemberService {
|
||||
spaceMember.id,
|
||||
dto.spaceId,
|
||||
);
|
||||
|
||||
this.auditService.log({
|
||||
event: AuditEvent.SPACE_MEMBER_REMOVED,
|
||||
resourceType: 'space_member',
|
||||
resourceId: dto.spaceId,
|
||||
changes: {
|
||||
before: { role: spaceMember.role },
|
||||
},
|
||||
metadata: {
|
||||
spaceId: dto.spaceId,
|
||||
spaceName: space.name,
|
||||
userId: spaceMember.userId,
|
||||
groupId: spaceMember.groupId,
|
||||
memberType: spaceMember.userId ? 'user' : 'group',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async updateSpaceMemberRole(
|
||||
@@ -259,6 +317,23 @@ export class SpaceMemberService {
|
||||
spaceMember.id,
|
||||
dto.spaceId,
|
||||
);
|
||||
|
||||
this.auditService.log({
|
||||
event: AuditEvent.SPACE_MEMBER_ROLE_CHANGED,
|
||||
resourceType: 'space_members',
|
||||
resourceId: dto.spaceId,
|
||||
changes: {
|
||||
before: { role: spaceMember.role },
|
||||
after: { role: dto.role },
|
||||
},
|
||||
metadata: {
|
||||
spaceId: dto.spaceId,
|
||||
spaceName: space.name,
|
||||
userId: spaceMember.userId,
|
||||
groupId: spaceMember.groupId,
|
||||
memberType: spaceMember.userId ? 'user' : 'group',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async validateLastAdmin(spaceId: string): Promise<void> {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Inject,
|
||||
Injectable,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
@@ -33,6 +34,11 @@ import {
|
||||
validateAllowedEmail,
|
||||
validateSsoEnforcement,
|
||||
} from '../../auth/auth.util';
|
||||
import { AuditEvent } from '../../../common/events/audit-events';
|
||||
import {
|
||||
AUDIT_SERVICE,
|
||||
IAuditService,
|
||||
} from '../../../integrations/audit/audit.service';
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceInvitationService {
|
||||
@@ -46,6 +52,7 @@ export class WorkspaceInvitationService {
|
||||
@InjectKysely() private readonly db: KyselyDB,
|
||||
@InjectQueue(QueueName.BILLING_QUEUE) private billingQueue: Queue,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
||||
) {}
|
||||
|
||||
async getInvitations(workspaceId: string, pagination: PaginationOptions) {
|
||||
@@ -179,6 +186,24 @@ export class WorkspaceInvitationService {
|
||||
workspace.hostname,
|
||||
);
|
||||
});
|
||||
|
||||
// Audit log for each invitation created
|
||||
for (const invitation of invites) {
|
||||
this.auditService.log({
|
||||
event: AuditEvent.WORKSPACE_INVITE_CREATED,
|
||||
resourceType: 'workspace_invitation',
|
||||
resourceId: invitation.id,
|
||||
changes: {
|
||||
after: {
|
||||
email: invitation.email,
|
||||
role: invitation.role,
|
||||
},
|
||||
},
|
||||
metadata: {
|
||||
groupIds: invitation.groupIds,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,11 +369,32 @@ export class WorkspaceInvitationService {
|
||||
invitationId: string,
|
||||
workspaceId: string,
|
||||
): Promise<void> {
|
||||
const invitation = await this.db
|
||||
.selectFrom('workspaceInvitations')
|
||||
.select(['id', 'email', 'role'])
|
||||
.where('id', '=', invitationId)
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.executeTakeFirst();
|
||||
|
||||
await this.db
|
||||
.deleteFrom('workspaceInvitations')
|
||||
.where('id', '=', invitationId)
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.execute();
|
||||
|
||||
if (invitation) {
|
||||
this.auditService.log({
|
||||
event: AuditEvent.WORKSPACE_INVITE_REVOKED,
|
||||
resourceType: 'workspace_invitation',
|
||||
resourceId: invitation.id,
|
||||
changes: {
|
||||
before: {
|
||||
email: invitation.email,
|
||||
role: invitation.role,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async getInvitationLinkById(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
Inject,
|
||||
Injectable,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
@@ -34,6 +35,11 @@ import { QueueJob, QueueName } from '../../../integrations/queue/constants';
|
||||
import { Queue } from 'bullmq';
|
||||
import { generateRandomSuffixNumbers } from '../../../common/helpers';
|
||||
import { isPageEmbeddingsTableExists } from '@docmost/db/helpers/helpers';
|
||||
import { AuditEvent } from '../../../common/events/audit-events';
|
||||
import {
|
||||
AUDIT_SERVICE,
|
||||
IAuditService,
|
||||
} from '../../../integrations/audit/audit.service';
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceService {
|
||||
@@ -52,6 +58,7 @@ export class WorkspaceService {
|
||||
@InjectQueue(QueueName.ATTACHMENT_QUEUE) private attachmentQueue: Queue,
|
||||
@InjectQueue(QueueName.BILLING_QUEUE) private billingQueue: Queue,
|
||||
@InjectQueue(QueueName.AI_QUEUE) private aiQueue: Queue,
|
||||
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
||||
) {}
|
||||
|
||||
async findById(workspaceId: string) {
|
||||
@@ -428,6 +435,20 @@ export class WorkspaceService {
|
||||
user.id,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
this.auditService.log({
|
||||
event: AuditEvent.USER_ROLE_CHANGED,
|
||||
resourceType: 'users',
|
||||
resourceId: user.id,
|
||||
changes: {
|
||||
before: { role: user.role },
|
||||
after: { role: newRole },
|
||||
},
|
||||
metadata: {
|
||||
userName: user.name,
|
||||
userEmail: user.email,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async generateHostname(
|
||||
@@ -531,6 +552,19 @@ export class WorkspaceService {
|
||||
.execute();
|
||||
});
|
||||
|
||||
this.auditService.log({
|
||||
event: AuditEvent.USER_DELETED,
|
||||
resourceType: 'users',
|
||||
resourceId: user.id,
|
||||
changes: {
|
||||
before: {
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
role: user.role,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await this.attachmentQueue.add(QueueJob.DELETE_USER_AVATARS, user);
|
||||
} catch (err) {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { Kysely, sql } from 'kysely';
|
||||
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.createTable('audit_logs')
|
||||
.addColumn('id', 'uuid', (col) =>
|
||||
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
|
||||
)
|
||||
.addColumn('workspace_id', 'uuid', (col) =>
|
||||
col.notNull().references('workspaces.id').onDelete('cascade'),
|
||||
)
|
||||
.addColumn('actor_id', 'uuid', (col) =>
|
||||
col.references('users.id').onDelete('set null'),
|
||||
)
|
||||
.addColumn('actor_type', 'varchar', (col) =>
|
||||
col.notNull().defaultTo('user'),
|
||||
)
|
||||
.addColumn('event', 'varchar', (col) => col.notNull())
|
||||
.addColumn('resource_type', 'varchar', (col) => col.notNull())
|
||||
.addColumn('resource_id', 'uuid')
|
||||
.addColumn('changes', 'jsonb')
|
||||
.addColumn('metadata', 'jsonb')
|
||||
.addColumn('ip_address', sql `inet`)
|
||||
.addColumn('created_at', 'timestamptz', (col) =>
|
||||
col.notNull().defaultTo(sql`now()`),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
await db.schema.dropTable('audit_logs').execute();
|
||||
}
|
||||
+28
-15
@@ -3,18 +3,13 @@
|
||||
* Please do not edit it manually.
|
||||
*/
|
||||
|
||||
import type { ColumnType } from 'kysely';
|
||||
import type { ColumnType } from "kysely";
|
||||
|
||||
export type Generated<T> =
|
||||
T extends ColumnType<infer S, infer I, infer U>
|
||||
? ColumnType<S, I | undefined, U>
|
||||
: ColumnType<T, T | undefined, T>;
|
||||
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
|
||||
? ColumnType<S, I | undefined, U>
|
||||
: ColumnType<T, T | undefined, T>;
|
||||
|
||||
export type Int8 = ColumnType<
|
||||
string,
|
||||
bigint | number | string,
|
||||
bigint | number | string
|
||||
>;
|
||||
export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>;
|
||||
|
||||
export type Json = JsonValue;
|
||||
|
||||
@@ -32,13 +27,13 @@ export type Timestamp = ColumnType<Date, Date | string, Date | string>;
|
||||
|
||||
export interface ApiKeys {
|
||||
createdAt: Generated<Timestamp>;
|
||||
creatorId: string;
|
||||
deletedAt: Timestamp | null;
|
||||
expiresAt: Timestamp | null;
|
||||
id: Generated<string>;
|
||||
lastUsedAt: Timestamp | null;
|
||||
name: string | null;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
creatorId: string;
|
||||
workspaceId: string;
|
||||
}
|
||||
|
||||
@@ -61,6 +56,20 @@ export interface Attachments {
|
||||
workspaceId: string;
|
||||
}
|
||||
|
||||
export interface AuditLogs {
|
||||
actorId: string | null;
|
||||
actorType: Generated<string>;
|
||||
changes: Json | null;
|
||||
createdAt: Generated<Timestamp>;
|
||||
event: string;
|
||||
id: Generated<string>;
|
||||
ipAddress: string | null;
|
||||
metadata: Json | null;
|
||||
resourceId: string | null;
|
||||
resourceType: string;
|
||||
workspaceId: string;
|
||||
}
|
||||
|
||||
export interface AuthAccounts {
|
||||
authProviderId: string | null;
|
||||
createdAt: Generated<Timestamp>;
|
||||
@@ -77,25 +86,25 @@ export interface AuthProviders {
|
||||
createdAt: Generated<Timestamp>;
|
||||
creatorId: string | null;
|
||||
deletedAt: Timestamp | null;
|
||||
groupSync: Generated<boolean>;
|
||||
id: Generated<string>;
|
||||
isEnabled: Generated<boolean>;
|
||||
groupSync: Generated<boolean>;
|
||||
ldapBaseDn: string | null;
|
||||
ldapBindDn: string | null;
|
||||
ldapBindPassword: string | null;
|
||||
ldapConfig: Json | null;
|
||||
ldapTlsCaCert: string | null;
|
||||
ldapTlsEnabled: Generated<boolean | null>;
|
||||
ldapUrl: string | null;
|
||||
ldapUserAttributes: Json | null;
|
||||
ldapUserSearchFilter: string | null;
|
||||
ldapConfig: Json | null;
|
||||
settings: Json | null;
|
||||
name: string;
|
||||
oidcClientId: string | null;
|
||||
oidcClientSecret: string | null;
|
||||
oidcIssuer: string | null;
|
||||
samlCertificate: string | null;
|
||||
samlUrl: string | null;
|
||||
settings: Json | null;
|
||||
type: string;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
workspaceId: string;
|
||||
@@ -225,9 +234,11 @@ export interface Pages {
|
||||
icon: string | null;
|
||||
id: Generated<string>;
|
||||
isLocked: Generated<boolean>;
|
||||
isRestricted: Generated<boolean>;
|
||||
lastUpdatedById: string | null;
|
||||
parentPageId: string | null;
|
||||
position: string | null;
|
||||
restrictedById: string | null;
|
||||
slugId: string;
|
||||
spaceId: string;
|
||||
textContent: string | null;
|
||||
@@ -298,12 +309,12 @@ export interface Users {
|
||||
deletedAt: Timestamp | null;
|
||||
email: string;
|
||||
emailVerifiedAt: Timestamp | null;
|
||||
hasGeneratedPassword: Generated<boolean | null>;
|
||||
id: Generated<string>;
|
||||
invitedById: string | null;
|
||||
lastActiveAt: Timestamp | null;
|
||||
lastLoginAt: Timestamp | null;
|
||||
locale: string | null;
|
||||
hasGeneratedPassword: Generated<boolean | null>;
|
||||
name: string | null;
|
||||
password: string | null;
|
||||
role: string | null;
|
||||
@@ -363,6 +374,7 @@ export interface Workspaces {
|
||||
export interface DB {
|
||||
apiKeys: ApiKeys;
|
||||
attachments: Attachments;
|
||||
auditLogs: AuditLogs;
|
||||
authAccounts: AuthAccounts;
|
||||
authProviders: AuthProviders;
|
||||
backlinks: Backlinks;
|
||||
@@ -372,6 +384,7 @@ export interface DB {
|
||||
groups: Groups;
|
||||
groupUsers: GroupUsers;
|
||||
pageHistory: PageHistory;
|
||||
AuditLog: PagePermissions;
|
||||
pages: Pages;
|
||||
shares: Shares;
|
||||
spaceMembers: SpaceMembers;
|
||||
|
||||
@@ -1,47 +1,6 @@
|
||||
import {
|
||||
ApiKeys,
|
||||
Attachments,
|
||||
AuthAccounts,
|
||||
AuthProviders,
|
||||
Backlinks,
|
||||
Billing,
|
||||
Comments,
|
||||
FileTasks,
|
||||
Groups,
|
||||
GroupUsers,
|
||||
PageHistory,
|
||||
Pages,
|
||||
Shares,
|
||||
SpaceMembers,
|
||||
Spaces,
|
||||
UserMfa,
|
||||
Users,
|
||||
UserTokens,
|
||||
WorkspaceInvitations,
|
||||
Workspaces,
|
||||
} from '@docmost/db/types/db';
|
||||
import { DB } from '@docmost/db/types/db';
|
||||
import { PageEmbeddings } from '@docmost/db/types/embeddings.types';
|
||||
|
||||
export interface DbInterface {
|
||||
attachments: Attachments;
|
||||
authAccounts: AuthAccounts;
|
||||
authProviders: AuthProviders;
|
||||
backlinks: Backlinks;
|
||||
billing: Billing;
|
||||
comments: Comments;
|
||||
fileTasks: FileTasks;
|
||||
groups: Groups;
|
||||
groupUsers: GroupUsers;
|
||||
export interface DbInterface extends DB {
|
||||
pageEmbeddings: PageEmbeddings;
|
||||
pageHistory: PageHistory;
|
||||
pages: Pages;
|
||||
shares: Shares;
|
||||
spaceMembers: SpaceMembers;
|
||||
spaces: Spaces;
|
||||
userMfa: UserMfa;
|
||||
users: Users;
|
||||
userTokens: UserTokens;
|
||||
workspaceInvitations: WorkspaceInvitations;
|
||||
workspaces: Workspaces;
|
||||
apiKeys: ApiKeys;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
FileTasks,
|
||||
UserMfa as _UserMFA,
|
||||
ApiKeys,
|
||||
AuditLogs,
|
||||
} from './db';
|
||||
import { PageEmbeddings } from '@docmost/db/types/embeddings.types';
|
||||
|
||||
@@ -131,3 +132,8 @@ export type UpdatableApiKey = Updateable<Omit<ApiKeys, 'id'>>;
|
||||
export type PageEmbedding = Selectable<PageEmbeddings>;
|
||||
export type InsertablePageEmbedding = Insertable<PageEmbeddings>;
|
||||
export type UpdatablePageEmbedding = Updateable<Omit<PageEmbeddings, 'id'>>;
|
||||
|
||||
// Audit Log
|
||||
export type AuditLog = Selectable<AuditLogs>;
|
||||
export type InsertableAuditLog = Insertable<AuditLogs>;
|
||||
export type UpdatableAuditLog = Updateable<Omit<AuditLogs, 'id'>>;
|
||||
|
||||
+1
-1
Submodule apps/server/src/ee updated: 075761c2d9...741c15eba3
@@ -0,0 +1,48 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AuditLogPayload, ActorType } from '../../common/events/audit-events';
|
||||
|
||||
export type IAuditService = {
|
||||
log(payload: AuditLogPayload): void | Promise<void>;
|
||||
logWithContext(
|
||||
payload: AuditLogPayload,
|
||||
context: {
|
||||
workspaceId: string;
|
||||
actorId?: string;
|
||||
actorType?: ActorType;
|
||||
ipAddress?: string;
|
||||
userAgent?: string;
|
||||
},
|
||||
): void | Promise<void>;
|
||||
setActorId(actorId: string): void;
|
||||
setActorType(actorType: ActorType): void;
|
||||
};
|
||||
|
||||
export const AUDIT_SERVICE = Symbol('AUDIT_SERVICE');
|
||||
|
||||
@Injectable()
|
||||
export class NoopAuditService implements IAuditService {
|
||||
log(_payload: AuditLogPayload): void {
|
||||
// No-op: swallow the log when EE module is not available
|
||||
}
|
||||
|
||||
logWithContext(
|
||||
_payload: AuditLogPayload,
|
||||
_context: {
|
||||
workspaceId: string;
|
||||
actorId?: string;
|
||||
actorType?: ActorType;
|
||||
ipAddress?: string;
|
||||
userAgent?: string;
|
||||
},
|
||||
): void {
|
||||
// No-op: swallow the log when EE module is not available
|
||||
}
|
||||
|
||||
setActorId(_actorId: string): void {
|
||||
// No-op
|
||||
}
|
||||
|
||||
setActorType(_actorType: ActorType): void {
|
||||
// No-op
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ export enum QueueName {
|
||||
FILE_TASK_QUEUE = '{file-task-queue}',
|
||||
SEARCH_QUEUE = '{search-queue}',
|
||||
AI_QUEUE = '{ai-queue}',
|
||||
AUDIT_QUEUE = '{audit-queue}',
|
||||
}
|
||||
|
||||
export enum QueueJob {
|
||||
@@ -58,4 +59,7 @@ export enum QueueJob {
|
||||
|
||||
GENERATE_PAGE_EMBEDDINGS = 'generate-page-embeddings',
|
||||
DELETE_PAGE_EMBEDDINGS = 'delete-page-embeddings',
|
||||
|
||||
AUDIT_LOG = 'audit-log',
|
||||
AUDIT_CLEANUP = 'audit-cleanup',
|
||||
}
|
||||
|
||||
@@ -73,6 +73,14 @@ import { BacklinksProcessor } from './processors/backlinks.processor';
|
||||
attempts: 1,
|
||||
},
|
||||
}),
|
||||
BullModule.registerQueue({
|
||||
name: QueueName.AUDIT_QUEUE,
|
||||
defaultJobOptions: {
|
||||
removeOnComplete: true,
|
||||
removeOnFail: true,
|
||||
attempts: 3,
|
||||
},
|
||||
}),
|
||||
],
|
||||
exports: [BullModule],
|
||||
providers: [BacklinksProcessor],
|
||||
|
||||
@@ -23,4 +23,3 @@ export * from "./lib/subpages";
|
||||
export * from "./lib/highlight";
|
||||
export * from "./lib/heading/heading";
|
||||
export * from "./lib/unique-id";
|
||||
export * from "./lib/columns";
|
||||
|
||||
@@ -1,152 +0,0 @@
|
||||
import { EditorState } from '@tiptap/pm/state';
|
||||
import { Decoration, DecorationSet, EditorView } from '@tiptap/pm/view';
|
||||
import { gridResizingPluginKey } from './state';
|
||||
import {
|
||||
draggedWidth,
|
||||
findBoundaryPosition,
|
||||
getColumnInfoAtPos,
|
||||
updateColumnNodeWidth,
|
||||
} from './utils';
|
||||
|
||||
function updateActiveHandle(view: EditorView, value: number) {
|
||||
view.dispatch(
|
||||
view.state.tr.setMeta(gridResizingPluginKey, {
|
||||
setHandle: value,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function handleMouseMove(
|
||||
view: EditorView,
|
||||
event: MouseEvent,
|
||||
handleWidth: number,
|
||||
): boolean {
|
||||
const pluginState = gridResizingPluginKey.getState(view.state);
|
||||
if (!pluginState) return false;
|
||||
|
||||
// TODO: limit call?
|
||||
|
||||
if (pluginState.dragging) return false;
|
||||
|
||||
const boundaryPos = findBoundaryPosition(view, event, handleWidth);
|
||||
if (boundaryPos !== pluginState.activeHandle) {
|
||||
updateActiveHandle(view, boundaryPos);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function handleMouseLeave(view: EditorView) {
|
||||
const pluginState = gridResizingPluginKey.getState(view.state);
|
||||
if (!pluginState) return false;
|
||||
if (pluginState.activeHandle > -1 && !pluginState.dragging) {
|
||||
updateActiveHandle(view, -1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function handleMouseDown(
|
||||
view: EditorView,
|
||||
event: MouseEvent,
|
||||
columnMinWidth: number,
|
||||
): boolean {
|
||||
const pluginState = gridResizingPluginKey.getState(view.state);
|
||||
if (!pluginState) return false;
|
||||
if (pluginState.activeHandle === -1) return false;
|
||||
if (pluginState.dragging) return false;
|
||||
|
||||
const columnInfo = getColumnInfoAtPos(view, pluginState.activeHandle);
|
||||
if (!columnInfo) return false;
|
||||
|
||||
const { domWidth, $pos, node } = columnInfo;
|
||||
const nodeAttrs = { ...(node.attrs || {}) };
|
||||
const nodePos = $pos.before();
|
||||
|
||||
view.dispatch(
|
||||
view.state.tr.setMeta(gridResizingPluginKey, {
|
||||
setDragging: { startX: event.clientX, startWidth: domWidth },
|
||||
}),
|
||||
);
|
||||
|
||||
const win = view.dom.ownerDocument.defaultView || window;
|
||||
|
||||
const finish = (e: MouseEvent) => {
|
||||
win.removeEventListener('mouseup', finish);
|
||||
win.removeEventListener('mousemove', move);
|
||||
|
||||
const pluginState = gridResizingPluginKey.getState(view.state);
|
||||
if (!pluginState?.dragging) return;
|
||||
|
||||
const finalWidth = draggedWidth(pluginState.dragging, e, columnMinWidth);
|
||||
updateColumnNodeWidth(view, nodePos, nodeAttrs, finalWidth);
|
||||
view.dispatch(
|
||||
view.state.tr.setMeta(gridResizingPluginKey, {
|
||||
setDragging: null,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const move = (e: MouseEvent) => {
|
||||
if (!e.buttons) {
|
||||
finish(e);
|
||||
return;
|
||||
}
|
||||
const pluginState = gridResizingPluginKey.getState(view.state);
|
||||
if (!pluginState?.dragging) return;
|
||||
|
||||
const newWidth = draggedWidth(pluginState.dragging, e, columnMinWidth);
|
||||
updateColumnNodeWidth(view, nodePos, nodeAttrs, newWidth);
|
||||
};
|
||||
|
||||
win.addEventListener('mouseup', finish);
|
||||
win.addEventListener('mousemove', move);
|
||||
|
||||
updateColumnNodeWidth(view, nodePos, nodeAttrs, domWidth);
|
||||
|
||||
event.preventDefault();
|
||||
return true;
|
||||
}
|
||||
|
||||
export function handleGridDecorations(
|
||||
state: EditorState,
|
||||
boundaryPos: number,
|
||||
): DecorationSet {
|
||||
const decorations = [];
|
||||
const $pos = state.doc.resolve(boundaryPos);
|
||||
if ($pos.nodeAfter !== null) {
|
||||
const widget = document.createElement('div');
|
||||
widget.className = 'grid-resize-handle';
|
||||
const circleButton = document.createElement('div');
|
||||
circleButton.className = 'circle-button';
|
||||
widget.appendChild(circleButton);
|
||||
const plusIcon = document.createElement('div');
|
||||
plusIcon.className = 'plus';
|
||||
circleButton.appendChild(plusIcon);
|
||||
decorations.push(Decoration.widget(boundaryPos, widget));
|
||||
}
|
||||
return DecorationSet.create(state.doc, decorations);
|
||||
}
|
||||
|
||||
export function handleMouseUp(view: EditorView, event: MouseEvent): boolean {
|
||||
const div = event.target as HTMLElement;
|
||||
if (!div) return false;
|
||||
if (div.className !== 'circle-button' && div.className !== 'plus')
|
||||
return false;
|
||||
const column = div.closest('.prosemirror-column');
|
||||
if (!column) return false;
|
||||
const boundryPos = view.posAtDOM(column, 0);
|
||||
if (!boundryPos) return false;
|
||||
const $pos = view.state.doc.resolve(boundryPos);
|
||||
const { state } = view;
|
||||
view.dispatch(
|
||||
state.tr.insert(
|
||||
$pos.pos + $pos.parent.nodeSize - 1,
|
||||
state.schema.nodes.column.create(
|
||||
{
|
||||
colWidth: 100,
|
||||
},
|
||||
state.schema.nodes.paragraph.create(),
|
||||
),
|
||||
),
|
||||
);
|
||||
return true;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export * from './schema';
|
||||
export * from './resize';
|
||||
export * from './keymap';
|
||||
export * from './tiptap';
|
||||
@@ -1,63 +0,0 @@
|
||||
import { liftTarget, canSplit } from "@tiptap/pm/transform";
|
||||
import { TextSelection, Command } from "@tiptap/pm/state";
|
||||
import {
|
||||
splitBlock,
|
||||
chainCommands,
|
||||
newlineInCode,
|
||||
createParagraphNear,
|
||||
} from "@tiptap/pm/commands";
|
||||
import { keymap } from "@tiptap/pm/keymap";
|
||||
import { ResolvedPos } from "@tiptap/pm/model";
|
||||
|
||||
function findParentColumn($pos: ResolvedPos) {
|
||||
for (let depth = $pos.depth; depth > 0; depth--) {
|
||||
const node = $pos.node(depth);
|
||||
if (node.type.name === "column") {
|
||||
return { node, depth };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export const liftEmptyBlock: Command = (state, dispatch) => {
|
||||
const { $cursor } = state.selection as TextSelection;
|
||||
if (!$cursor || $cursor.parent.content.size) return false;
|
||||
if ("column" === $cursor.node($cursor.depth - 1).type.name) return false;
|
||||
if ($cursor.depth > 1 && $cursor.after() != $cursor.end(-1)) {
|
||||
const before = $cursor.before();
|
||||
if (canSplit(state.doc, before)) {
|
||||
if (dispatch) dispatch(state.tr.split(before).scrollIntoView());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
const range = $cursor.blockRange(),
|
||||
target = range && liftTarget(range);
|
||||
if (target == null) return false;
|
||||
if (dispatch) dispatch(state.tr.lift(range!, target).scrollIntoView());
|
||||
return true;
|
||||
};
|
||||
|
||||
export const columnsKeymap = keymap({
|
||||
Enter: chainCommands(
|
||||
newlineInCode,
|
||||
createParagraphNear,
|
||||
liftEmptyBlock,
|
||||
splitBlock,
|
||||
),
|
||||
"Mod-a": (state, dispatch, view) => {
|
||||
const { selection } = state;
|
||||
const { $from } = selection;
|
||||
const found = findParentColumn($from);
|
||||
if (found) {
|
||||
const { depth } = found;
|
||||
const start = $from.start(depth);
|
||||
const end = $from.end(depth);
|
||||
const tr = state.tr.setSelection(
|
||||
TextSelection.create(state.doc, start, end),
|
||||
);
|
||||
if (dispatch) dispatch(tr);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
} as { [key: string]: Command });
|
||||
@@ -1,62 +0,0 @@
|
||||
import { Plugin } from '@tiptap/pm/state';
|
||||
import {
|
||||
handleGridDecorations,
|
||||
handleMouseDown,
|
||||
handleMouseLeave,
|
||||
handleMouseMove,
|
||||
handleMouseUp,
|
||||
} from './dom';
|
||||
import { GridResizeState, gridResizingPluginKey } from './state';
|
||||
|
||||
export function gridResizingPlugin(options?: {
|
||||
handleWidth?: number;
|
||||
columnMinWidth?: number;
|
||||
}) {
|
||||
const handleWidth = options?.handleWidth ?? 2;
|
||||
const columnMinWidth = options?.columnMinWidth ?? 50;
|
||||
|
||||
return new Plugin<GridResizeState>({
|
||||
key: gridResizingPluginKey,
|
||||
|
||||
state: {
|
||||
init: () => new GridResizeState(-1, false),
|
||||
apply: (tr, prev) => {
|
||||
return prev.apply(tr);
|
||||
},
|
||||
},
|
||||
|
||||
props: {
|
||||
attributes: (state): Record<string, string> => {
|
||||
const pluginState = gridResizingPluginKey.getState(state);
|
||||
if (pluginState && pluginState.activeHandle > -1) {
|
||||
return { class: 'resize-cursor' };
|
||||
}
|
||||
return {};
|
||||
},
|
||||
|
||||
// The main event handlers
|
||||
handleDOMEvents: {
|
||||
mousemove: (view, event: MouseEvent) => {
|
||||
return handleMouseMove(view, event, handleWidth);
|
||||
},
|
||||
mouseleave: (view) => {
|
||||
return handleMouseLeave(view);
|
||||
},
|
||||
mousedown: (view, event: MouseEvent) => {
|
||||
return handleMouseDown(view, event, columnMinWidth);
|
||||
},
|
||||
mouseup: (view, event: MouseEvent) => {
|
||||
return handleMouseUp(view, event);
|
||||
},
|
||||
},
|
||||
|
||||
decorations: (state) => {
|
||||
const pluginState = gridResizingPluginKey.getState(state);
|
||||
if (!pluginState) return null;
|
||||
if (pluginState.activeHandle === -1) return null;
|
||||
|
||||
return handleGridDecorations(state, pluginState.activeHandle);
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
import { NodeSpec } from '@tiptap/pm/model';
|
||||
|
||||
export type ColumnNodes = Record<'column' | 'column_container', NodeSpec>;
|
||||
|
||||
export function columnNodes(): ColumnNodes {
|
||||
return {
|
||||
column: {
|
||||
group: 'block',
|
||||
content: 'block+',
|
||||
attrs: {
|
||||
colWidth: { default: 200 },
|
||||
},
|
||||
parseDOM: [
|
||||
{
|
||||
tag: 'div.prosemirror-column',
|
||||
getAttrs(dom) {
|
||||
if (!(dom instanceof HTMLElement)) return false;
|
||||
const width = dom.style.width.replace('px', '') || 200;
|
||||
return {
|
||||
colWidth: width,
|
||||
};
|
||||
},
|
||||
},
|
||||
],
|
||||
toDOM(node) {
|
||||
const { colWidth } = node.attrs;
|
||||
const style = colWidth ? `width: ${colWidth}px;` : '';
|
||||
return [
|
||||
'div',
|
||||
{
|
||||
class: 'prosemirror-column',
|
||||
style,
|
||||
},
|
||||
0,
|
||||
];
|
||||
},
|
||||
},
|
||||
column_container: {
|
||||
group: 'block',
|
||||
content: 'column+',
|
||||
parseDOM: [
|
||||
{
|
||||
tag: 'div.prosemirror-column-container',
|
||||
},
|
||||
],
|
||||
toDOM() {
|
||||
return ['div', { class: 'prosemirror-column-container' }, 0];
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { PluginKey, Transaction } from '@tiptap/pm/state';
|
||||
|
||||
export const gridResizingPluginKey = new PluginKey<GridResizeState>(
|
||||
'gridResizingPlugin',
|
||||
);
|
||||
|
||||
export type Dragging = {
|
||||
startX: number;
|
||||
startWidth: number;
|
||||
};
|
||||
|
||||
export class GridResizeState {
|
||||
constructor(
|
||||
public activeHandle: number,
|
||||
public dragging: Dragging | false,
|
||||
) {}
|
||||
|
||||
apply(tr: Transaction): GridResizeState {
|
||||
const action = tr.getMeta(gridResizingPluginKey);
|
||||
if (!action) return this;
|
||||
|
||||
if (typeof action.setHandle === 'number') {
|
||||
return new GridResizeState(action.setHandle, false);
|
||||
}
|
||||
if (action.setDragging !== undefined) {
|
||||
return new GridResizeState(this.activeHandle, action.setDragging);
|
||||
}
|
||||
if (this.activeHandle > -1 && tr.docChanged) {
|
||||
// remap when doc changes
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
import { Node, mergeAttributes, Extension } from '@tiptap/core';
|
||||
import { columnsKeymap } from './keymap';
|
||||
import { gridResizingPlugin } from './resize';
|
||||
|
||||
const Column = Node.create({
|
||||
name: 'column',
|
||||
|
||||
group: 'block',
|
||||
content: 'block+',
|
||||
|
||||
addAttributes() {
|
||||
return {
|
||||
colWidth: {
|
||||
default: 200,
|
||||
parseHTML: (element) => {
|
||||
const width = (element as HTMLElement).style.width.replace('px', '');
|
||||
return Number(width) || 200;
|
||||
},
|
||||
renderHTML: (attributes) => {
|
||||
const style = attributes.colWidth
|
||||
? `width: ${attributes.colWidth}px;`
|
||||
: '';
|
||||
return { style };
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
parseHTML() {
|
||||
return [
|
||||
{
|
||||
tag: 'div.prosemirror-column',
|
||||
},
|
||||
];
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
return [
|
||||
'div',
|
||||
mergeAttributes(HTMLAttributes, { class: 'prosemirror-column' }),
|
||||
0,
|
||||
];
|
||||
},
|
||||
});
|
||||
|
||||
const ColumnContainer = Node.create({
|
||||
name: 'column_container',
|
||||
|
||||
group: 'block',
|
||||
content: 'column+',
|
||||
|
||||
parseHTML() {
|
||||
return [
|
||||
{
|
||||
tag: 'div.prosemirror-column-container',
|
||||
},
|
||||
];
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
return [
|
||||
'div',
|
||||
mergeAttributes(HTMLAttributes, {
|
||||
class: 'prosemirror-column-container',
|
||||
}),
|
||||
0,
|
||||
];
|
||||
},
|
||||
});
|
||||
|
||||
export const ColumnsExtension = Extension.create({
|
||||
name: 'columns',
|
||||
|
||||
addExtensions() {
|
||||
return [Column, ColumnContainer];
|
||||
},
|
||||
|
||||
addProseMirrorPlugins() {
|
||||
return [
|
||||
gridResizingPlugin({ handleWidth: 2, columnMinWidth: 50 }),
|
||||
columnsKeymap,
|
||||
];
|
||||
},
|
||||
});
|
||||
@@ -1,75 +0,0 @@
|
||||
import { EditorView } from '@tiptap/pm/view';
|
||||
import { type Dragging } from './state';
|
||||
|
||||
export function findBoundaryPosition(
|
||||
view: EditorView,
|
||||
event: MouseEvent,
|
||||
handleWidth: number,
|
||||
): number {
|
||||
const gridDOM = event
|
||||
.composedPath()
|
||||
.find((el) =>
|
||||
(el as HTMLElement).classList?.contains('prosemirror-column-container'),
|
||||
) as HTMLElement | undefined;
|
||||
if (!gridDOM) return -1;
|
||||
|
||||
const children = Array.from(gridDOM.children).filter((el) =>
|
||||
el.classList.contains('prosemirror-column'),
|
||||
);
|
||||
for (let i = 0; i < children.length; i++) {
|
||||
const colEl = children[i] as HTMLElement;
|
||||
const rect = colEl.getBoundingClientRect();
|
||||
if (
|
||||
event.clientX >= rect.right - handleWidth - 2 &&
|
||||
event.clientX <= rect.right + 10 + handleWidth
|
||||
) {
|
||||
const pos = view.posAtDOM(colEl, 0);
|
||||
if (pos != null) {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
export function draggedWidth(
|
||||
dragging: Dragging,
|
||||
event: MouseEvent,
|
||||
minWidth: number,
|
||||
): number {
|
||||
const offset = event.clientX - dragging.startX;
|
||||
return Math.max(minWidth, dragging.startWidth + offset);
|
||||
}
|
||||
|
||||
export function updateColumnNodeWidth(
|
||||
view: EditorView,
|
||||
pos: number,
|
||||
attrs: Record<string, string>,
|
||||
width: number,
|
||||
) {
|
||||
view.dispatch(
|
||||
view.state.tr.setNodeMarkup(pos, undefined, {
|
||||
...attrs,
|
||||
colWidth: width - 12 * 2,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function getColumnInfoAtPos(view: EditorView, boundaryPos: number) {
|
||||
const $pos = view.state.doc.resolve(boundaryPos);
|
||||
const node = $pos.parent;
|
||||
if (!node || node.type.name !== 'column') return null;
|
||||
|
||||
const dom = view.domAtPos($pos.pos);
|
||||
if (!dom.node) return null;
|
||||
|
||||
const columnEl =
|
||||
dom.node instanceof HTMLElement
|
||||
? dom.node
|
||||
: (dom.node.childNodes[dom.offset] as HTMLElement);
|
||||
|
||||
const domWidth = columnEl.offsetWidth;
|
||||
|
||||
return { $pos, node, columnEl, domWidth };
|
||||
}
|
||||
Generated
+19
@@ -578,6 +578,9 @@ importers:
|
||||
nanoid:
|
||||
specifier: 3.3.11
|
||||
version: 3.3.11
|
||||
nestjs-cls:
|
||||
specifier: ^4.5.0
|
||||
version: 4.5.0(@nestjs/common@11.1.9(class-transformer@0.5.1)(class-validator@0.14.3)(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.1.9)(reflect-metadata@0.2.2)(rxjs@7.8.2)
|
||||
nestjs-kysely:
|
||||
specifier: ^1.2.0
|
||||
version: 1.2.0(@nestjs/common@11.1.9(class-transformer@0.5.1)(class-validator@0.14.3)(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.1.9)(kysely@0.28.2)(reflect-metadata@0.2.2)
|
||||
@@ -7982,6 +7985,15 @@ packages:
|
||||
neo-async@2.6.2:
|
||||
resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==}
|
||||
|
||||
nestjs-cls@4.5.0:
|
||||
resolution: {integrity: sha512-oi3GNCc5pnsnVI5WJKMDwmg4NP+JyEw+edlwgepyUba5+RGGtJzpbVaaxXGW1iPbDuQde3/fA8Jdjq9j88BVcQ==}
|
||||
engines: {node: '>=16'}
|
||||
peerDependencies:
|
||||
'@nestjs/common': '> 7.0.0 < 11'
|
||||
'@nestjs/core': '> 7.0.0 < 11'
|
||||
reflect-metadata: '*'
|
||||
rxjs: '>= 7'
|
||||
|
||||
nestjs-kysely@1.2.0:
|
||||
resolution: {integrity: sha512-KseCGb0SXCzIYC+Hx3Z3d+kPAfSZCSK6j9UoqUV/gcBCPad9utC7itmoUw0/w5sV+Jf9pc1DKpgClP1IkflA4w==}
|
||||
peerDependencies:
|
||||
@@ -19131,6 +19143,13 @@ snapshots:
|
||||
|
||||
neo-async@2.6.2: {}
|
||||
|
||||
nestjs-cls@4.5.0(@nestjs/common@11.1.9(class-transformer@0.5.1)(class-validator@0.14.3)(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.1.9)(reflect-metadata@0.2.2)(rxjs@7.8.2):
|
||||
dependencies:
|
||||
'@nestjs/common': 11.1.9(class-transformer@0.5.1)(class-validator@0.14.3)(reflect-metadata@0.2.2)(rxjs@7.8.2)
|
||||
'@nestjs/core': 11.1.9(@nestjs/common@11.1.9(class-transformer@0.5.1)(class-validator@0.14.3)(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/websockets@11.1.9)(reflect-metadata@0.2.2)(rxjs@7.8.2)
|
||||
reflect-metadata: 0.2.2
|
||||
rxjs: 7.8.2
|
||||
|
||||
nestjs-kysely@1.2.0(@nestjs/common@11.1.9(class-transformer@0.5.1)(class-validator@0.14.3)(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.1.9)(kysely@0.28.2)(reflect-metadata@0.2.2):
|
||||
dependencies:
|
||||
'@nestjs/common': 11.1.9(class-transformer@0.5.1)(class-validator@0.14.3)(reflect-metadata@0.2.2)(rxjs@7.8.2)
|
||||
|
||||
Reference in New Issue
Block a user