- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.4k
 
Improve code block language selection #15966
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 
          
            
          
           | 
    @@ -1236,29 +1236,33 @@ export class Toolbar { | |||||||||||||||||||||
| hideElements(["hint"], protyle); | ||||||||||||||||||||||
| window.siyuan.menus.menu.remove(); | ||||||||||||||||||||||
| this.range = getEditorRange(nodeElement); | ||||||||||||||||||||||
| let html = `<div class="b3-list-item">${window.siyuan.languages.clear}</div>`; | ||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||
| this.subElement.style.width = ""; | ||||||||||||||||||||||
| this.subElement.style.padding = ""; | ||||||||||||||||||||||
| this.subElement.innerHTML = `<div data-id="codeLanguage" class="fn__flex-column" style="max-height:50vh"> | ||||||||||||||||||||||
| <input placeholder="${window.siyuan.languages.search}" style="margin: 0 8px 4px 8px" class="b3-text-field"/> | ||||||||||||||||||||||
| <div class="b3-list fn__flex-1 b3-list--background" style="position: relative"></div> | ||||||||||||||||||||||
| </div>`; | ||||||||||||||||||||||
| const listElement = this.subElement.lastElementChild.lastElementChild as HTMLElement; | ||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||
| let html = `<div data-id="clearLanguage" class="b3-list-item">${window.siyuan.languages.clear}</div>`; | ||||||||||||||||||||||
| let hljsLanguages = Constants.ALIAS_CODE_LANGUAGES.concat(window.hljs?.listLanguages() ?? []).sort(); | ||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||
| const eventDetail = {languages: hljsLanguages}; | ||||||||||||||||||||||
| const eventDetail = {languages: hljsLanguages, type: "init", listElement}; | ||||||||||||||||||||||
| if (protyle.app && protyle.app.plugins) { | ||||||||||||||||||||||
| protyle.app.plugins.forEach((plugin: any) => { | ||||||||||||||||||||||
| plugin.eventBus.emit("code-language-update", eventDetail); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||
| hljsLanguages = eventDetail.languages; | ||||||||||||||||||||||
| hljsLanguages.forEach((item, index) => { | ||||||||||||||||||||||
| html += `<div class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">${item}</div>`; | ||||||||||||||||||||||
| hljsLanguages.forEach((item) => { | ||||||||||||||||||||||
| html += `<div data-id="${item}" class="b3-list-item">${item}</div>`; | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||
| this.subElement.style.width = ""; | ||||||||||||||||||||||
| this.subElement.style.padding = ""; | ||||||||||||||||||||||
| this.subElement.innerHTML = `<div class="fn__flex-column" style="max-height:50vh"> | ||||||||||||||||||||||
| <input placeholder="${window.siyuan.languages.search}" style="margin: 0 8px 4px 8px" class="b3-text-field"/> | ||||||||||||||||||||||
| <div class="b3-list fn__flex-1 b3-list--background" style="position: relative">${html}</div> | ||||||||||||||||||||||
| </div>`; | ||||||||||||||||||||||
| listElement.innerHTML = html; | ||||||||||||||||||||||
| listElement.firstElementChild.nextElementSibling.classList.add("b3-list-item--focus"); | ||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||
| const listElement = this.subElement.lastElementChild.lastElementChild as HTMLElement; | ||||||||||||||||||||||
| const inputElement = this.subElement.querySelector("input"); | ||||||||||||||||||||||
| inputElement.addEventListener("keydown", (event: KeyboardEvent) => { | ||||||||||||||||||||||
| event.stopPropagation(); | ||||||||||||||||||||||
| 
        
          
        
         | 
    @@ -1269,63 +1273,82 @@ export class Toolbar { | |||||||||||||||||||||
| if (event.key === "Enter") { | ||||||||||||||||||||||
| this.updateLanguage(languageElements, protyle, this.subElement.querySelector(".b3-list-item--focus").textContent); | ||||||||||||||||||||||
| event.preventDefault(); | ||||||||||||||||||||||
| event.stopPropagation(); | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if (event.key === "Escape") { | ||||||||||||||||||||||
| this.subElement.classList.add("fn__none"); | ||||||||||||||||||||||
| focusByRange(this.range); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||
| const highlightText = (text: string, search: string) => { | ||||||||||||||||||||||
| // 转义正则特殊字符 | ||||||||||||||||||||||
| const escapedSearch = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||||||||||||||||||||||
| // 创建不区分大小写的正则表达式 | ||||||||||||||||||||||
| const regex = new RegExp(escapedSearch, "gi"); | ||||||||||||||||||||||
| // 替换匹配内容并保留原始大小写 | ||||||||||||||||||||||
| 
         
      Comment on lines
    
      +1285
     to 
      +1289
    
   
  
    
 | 
||||||||||||||||||||||
| // 转义正则特殊字符 | |
| const escapedSearch = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| // 创建不区分大小写的正则表达式 | |
| const regex = new RegExp(escapedSearch, "gi"); | |
| // 替换匹配内容并保留原始大小写 | |
| // Escape regex special characters | |
| const escapedSearch = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| // Create case-insensitive regex | |
| const regex = new RegExp(escapedSearch, "gi"); | |
| // Replace matches while preserving original case | 
    
      
    
      Copilot
AI
    
    
    
      Oct 20, 2025 
    
  
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The inline comment '不区分大小写' is in Chinese. Consider translating to English: 'Case-insensitive comparison'.
| // 不区分大小写 | |
| // Case-insensitive comparison | 
    
      
    
      Copilot
AI
    
    
    
      Oct 20, 2025 
    
  
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The inline comment '两者都匹配开头时,短字符串优先' is in Chinese. Consider translating to English: 'When both match the prefix, shorter strings take priority'.
| // 两者都匹配开头时,短字符串优先 | |
| // When both match the prefix, shorter strings take priority | 
    
      
    
      Copilot
AI
    
    
    
      Oct 20, 2025 
    
  
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The inline comment '都不匹配时保持原顺序' is in Chinese. Consider translating to English: 'Maintain original order when neither matches'.
| // 都不匹配时保持原顺序 | |
| // Maintain original order when neither matches | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
event.stopPropagation()call was removed from line 1276 after the Enter key handler. This could allow the event to bubble up unexpectedly. Consider whether event propagation should still be stopped here to maintain consistent behavior with the Escape key handler on line 1281.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TCOTC 移除这个的原因是?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Vanessa219 前面有,后面多余的就删了