mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 18:46:19 +08:00
fix(editor): database behavier (#14394)
fix #13459 fix #13707 fix #13924 #### PR Dependency Tree * **PR #14394** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Improved URL paste: text is split into segments, inserted correctly, and single-URL pastes create linked-page references. * **UI Improvements** * Redesigned layout selector with compact dynamic options. * Number-format options are always available in table headers and mobile menus. * **Bug Fixes** * More consistent paste behavior for mixed text+URL content. * Prevented recursive selection updates when exiting edit mode. * **Tests** * Added tests for URL splitting, paste insertion, number formatting, and selection behavior. * **Chores** * Removed number-formatting feature flag; formatting now applied by default. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import { isValidUrl } from '../../utils/url.js';
|
||||
import { isValidUrl, splitTextByUrl } from '../../utils/url.js';
|
||||
|
||||
describe('isValidUrl: determining whether a URL is valid is very complicated', () => {
|
||||
test('basic case', () => {
|
||||
@@ -85,3 +85,55 @@ describe('isValidUrl: determining whether a URL is valid is very complicated', (
|
||||
expect(isValidUrl('http://127.0.0.1', 'http://127.0.0.1')).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('splitTextByUrl', () => {
|
||||
test('should split text and keep url part as link segment', () => {
|
||||
expect(splitTextByUrl('hi - https://google.com')).toEqual([
|
||||
{ text: 'hi - ' },
|
||||
{
|
||||
text: 'https://google.com',
|
||||
link: 'https://google.com',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('should support prefixed url token without swallowing prefix text', () => {
|
||||
expect(splitTextByUrl('-https://google.com')).toEqual([
|
||||
{ text: '-' },
|
||||
{
|
||||
text: 'https://google.com',
|
||||
link: 'https://google.com',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('should trim tail punctuations from url token', () => {
|
||||
expect(splitTextByUrl('visit https://google.com, now')).toEqual([
|
||||
{ text: 'visit ' },
|
||||
{
|
||||
text: 'https://google.com',
|
||||
link: 'https://google.com',
|
||||
},
|
||||
{ text: ', now' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('should convert domain token in plain text', () => {
|
||||
expect(splitTextByUrl('google.com and text')).toEqual([
|
||||
{
|
||||
text: 'google.com',
|
||||
link: 'https://google.com',
|
||||
},
|
||||
{ text: ' and text' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('should normalize www domain token link while preserving display text', () => {
|
||||
expect(splitTextByUrl('www.google.com')).toEqual([
|
||||
{
|
||||
text: 'www.google.com',
|
||||
link: 'https://www.google.com',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,6 @@ import { type Store, StoreExtension } from '@blocksuite/store';
|
||||
import { type Signal, signal } from '@preact/signals-core';
|
||||
|
||||
export interface BlockSuiteFlags {
|
||||
enable_database_number_formatting: boolean;
|
||||
enable_database_attachment_note: boolean;
|
||||
enable_database_full_width: boolean;
|
||||
enable_block_query: boolean;
|
||||
@@ -28,7 +27,6 @@ export class FeatureFlagService extends StoreExtension {
|
||||
static override key = 'feature-flag-server';
|
||||
|
||||
private readonly _flags: Signal<BlockSuiteFlags> = signal({
|
||||
enable_database_number_formatting: false,
|
||||
enable_database_attachment_note: false,
|
||||
enable_database_full_width: false,
|
||||
enable_block_query: false,
|
||||
|
||||
@@ -95,28 +95,107 @@ export function isValidUrl(str: string, baseUrl = location.origin) {
|
||||
return result?.allowed ?? false;
|
||||
}
|
||||
|
||||
const URL_SCHEME_IN_TOKEN_REGEXP =
|
||||
/(?:https?:\/\/|ftp:\/\/|sftp:\/\/|mailto:|tel:|www\.)/i;
|
||||
|
||||
const URL_LEADING_DELIMITER_REGEXP = /^[-([{<'"~]+/;
|
||||
|
||||
const URL_TRAILING_DELIMITER_REGEXP = /[)\]}>.,;:!?'"]+$/;
|
||||
|
||||
export type UrlTextSegment = {
|
||||
text: string;
|
||||
link?: string;
|
||||
};
|
||||
|
||||
function appendUrlTextSegment(
|
||||
segments: UrlTextSegment[],
|
||||
segment: UrlTextSegment
|
||||
) {
|
||||
if (!segment.text) return;
|
||||
const last = segments[segments.length - 1];
|
||||
if (last && !last.link && !segment.link) {
|
||||
last.text += segment.text;
|
||||
return;
|
||||
}
|
||||
segments.push(segment);
|
||||
}
|
||||
|
||||
function splitTokenByUrl(token: string, baseUrl: string): UrlTextSegment[] {
|
||||
const schemeMatch = token.match(URL_SCHEME_IN_TOKEN_REGEXP);
|
||||
const schemeIndex = schemeMatch?.index;
|
||||
if (typeof schemeIndex === 'number' && schemeIndex > 0) {
|
||||
return [
|
||||
{ text: token.slice(0, schemeIndex) },
|
||||
...splitTokenByUrl(token.slice(schemeIndex), baseUrl),
|
||||
];
|
||||
}
|
||||
|
||||
const leading = token.match(URL_LEADING_DELIMITER_REGEXP)?.[0] ?? '';
|
||||
const withoutLeading = token.slice(leading.length);
|
||||
const trailing =
|
||||
withoutLeading.match(URL_TRAILING_DELIMITER_REGEXP)?.[0] ?? '';
|
||||
const core = trailing
|
||||
? withoutLeading.slice(0, withoutLeading.length - trailing.length)
|
||||
: withoutLeading;
|
||||
|
||||
if (core && isValidUrl(core, baseUrl)) {
|
||||
const segments: UrlTextSegment[] = [];
|
||||
appendUrlTextSegment(segments, { text: leading });
|
||||
appendUrlTextSegment(segments, { text: core, link: normalizeUrl(core) });
|
||||
appendUrlTextSegment(segments, { text: trailing });
|
||||
return segments;
|
||||
}
|
||||
|
||||
return [{ text: token }];
|
||||
}
|
||||
|
||||
/**
|
||||
* Split plain text into mixed segments, where only URL segments carry link metadata.
|
||||
* This is used by paste handlers so text like `example:https://google.com` keeps
|
||||
* normal text while only URL parts are linkified.
|
||||
*/
|
||||
export function splitTextByUrl(text: string, baseUrl = location.origin) {
|
||||
const chunks = text.match(/\s+|\S+/g);
|
||||
if (!chunks) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const segments: UrlTextSegment[] = [];
|
||||
chunks.forEach(chunk => {
|
||||
if (/^\s+$/.test(chunk)) {
|
||||
appendUrlTextSegment(segments, { text: chunk });
|
||||
return;
|
||||
}
|
||||
splitTokenByUrl(chunk, baseUrl).forEach(segment => {
|
||||
appendUrlTextSegment(segments, segment);
|
||||
});
|
||||
});
|
||||
return segments;
|
||||
}
|
||||
|
||||
// https://en.wikipedia.org/wiki/Top-level_domain
|
||||
const COMMON_TLDS = new Set([
|
||||
'com',
|
||||
'org',
|
||||
'net',
|
||||
'edu',
|
||||
'gov',
|
||||
'co',
|
||||
'io',
|
||||
'me',
|
||||
'moe',
|
||||
'mil',
|
||||
'top',
|
||||
'dev',
|
||||
'xyz',
|
||||
'info',
|
||||
'cat',
|
||||
'ru',
|
||||
'co',
|
||||
'com',
|
||||
'de',
|
||||
'dev',
|
||||
'edu',
|
||||
'eu',
|
||||
'gov',
|
||||
'info',
|
||||
'io',
|
||||
'jp',
|
||||
'uk',
|
||||
'me',
|
||||
'mil',
|
||||
'moe',
|
||||
'net',
|
||||
'org',
|
||||
'pro',
|
||||
'ru',
|
||||
'top',
|
||||
'uk',
|
||||
'xyz',
|
||||
]);
|
||||
|
||||
function isCommonTLD(url: URL) {
|
||||
|
||||
Reference in New Issue
Block a user