Skip to content

Commit

Permalink
feat: add popup background transparent setting
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcpy committed Nov 15, 2024
1 parent e72fb6f commit 0d68e5d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
49 changes: 48 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ interface MermaidPopupSetting {

open_btn_pos_x:string;
open_btn_pos_y:string;
bgColorLight:string;
bgColorDark:string;
bgAlpha:string;
bgAlphaStep:Record<string, string>;
};

const DEFAULT_SETTINGS: MermaidPopupSetting = {
Expand Down Expand Up @@ -66,6 +70,22 @@ const DEFAULT_SETTINGS: MermaidPopupSetting = {
},
open_btn_pos_x:'35',
open_btn_pos_y:'90',
bgColorLight:'rgba(255,255,255, 0.5)',
bgColorDark:'rgba(51,51,51, 0.5)',
bgAlpha:'0.5',
bgAlphaStep:{
'0.0':'0.0',
'0.1':'0.1',
'0.2':'0.2',
'0.3':'0.3',
'0.4':'0.4',
'0.5':'0.5',
'0.6':'0.6',
'0.7':'0.7',
'0.8':'0.8',
'0.9':'0.9',
'1.0':'1.0'
},
};

export default class MermaidPopupPlugin extends Plugin {
Expand Down Expand Up @@ -420,7 +440,7 @@ export default class MermaidPopupPlugin extends Plugin {
// popup-overlay
const overlay = targetElement.doc.createElement('div');
overlay.classList.add('popup-overlay');

this.setPopupBgAlpha(overlay);
// copy target
let targetElementClone = targetElement.cloneNode(true);
let targetElementInPopup = targetElementClone as HTMLElement;
Expand Down Expand Up @@ -478,6 +498,33 @@ export default class MermaidPopupPlugin extends Plugin {
});
}

setPopupBgAlpha(_popupElement:HTMLElement) {
if (!_popupElement)
return;

let alpha = this.settings.bgAlpha;
// 构造新的 rgba 值
let newBgColor;
if (this.isThemeLight()) {
newBgColor = `rgba(255, 255, 255, ${alpha})`;
} else if (this.isThemeDark()) {
newBgColor = `rgba(51, 51, 51, ${alpha})`;
}

// 更新背景颜色和模糊效果
_popupElement.setCssStyles({
backgroundColor: newBgColor
})
}

isThemeLight(){
return document.body.classList.contains('theme-light');
}

isThemeDark(){
return document.body.classList.contains('theme-dark');
}

setPopupSize(_targetElementInPopup:HTMLElement, _targetElement:HTMLElement){
let multiVal = parseFloat(this.settings.PopupSizeInitValue);
if (typeof multiVal != "number"){
Expand Down
38 changes: 37 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,34 @@ class MermaidPopupSettingTab extends PluginSettingTab {
)
});

const row = tbody.createEl('tr');
const td_title_a = row.createEl('td');
// bg Alpha title
let titlePpBgA = td_title_a.createEl('h2', { text: 'Popup Background Alpha Value' });
titlePpBgA.classList.add('config-text');
//const rowPpBgAlpha = td_title_a.createDiv({ cls: 'kv-row' });
// bg Alpha settign
new Setting(td_title_a)
.setName('Choose the alpha value')
.addDropdown(dropdown => {
let bgAlphaStep = this.plugin.settings.bgAlphaStep;
for(const key in bgAlphaStep){
dropdown.addOption(key, bgAlphaStep[key])
}
dropdown
.setValue(this.plugin.settings.bgAlpha)
.onChange(async (value) => {
this.plugin.settings.bgAlpha = value;
await this.plugin.saveSettings();
}

)
});

this.addClass(td_title_a, 'setting-item', 'setting-item-on-top-line');



// 开启弹窗按钮位置
let title_btn_pos = containerEl.createEl('h2', { text: 'Open Popup Button Relative Position Init' });
title_btn_pos.classList.add('config-text');
Expand All @@ -149,8 +177,9 @@ class MermaidPopupSettingTab extends PluginSettingTab {
this.setInfo(kvRow_open_btn, 'Click for tips on Open Popup Button Relative Position Init Setting.',
'Open Popup Button Relative Position Init Setting',
'x represents the pixels to the right edge of the diagram container.'
)
)

// Add New Diagram
let title = containerEl.createEl('h2', { text: 'Add New Diagram' });
title.classList.add('config-text');

Expand Down Expand Up @@ -226,6 +255,13 @@ class MermaidPopupSettingTab extends PluginSettingTab {
containerEl.createEl('p', { text: 'if you add it to the class list of your target container, it will get the functionality.' });
}

addClass(_container:HTMLElement, _targetElementClass:string, _class:string){
let dropdownElement = _container.querySelector('.' + _targetElementClass);
if (dropdownElement) {
dropdownElement.classList.add(_class);
}
}

setInfo(containerEl: HTMLElement, tip:string, title:string, msg:string){
const addSettings = new Setting(containerEl);
addSettings.addExtraButton((extra) => {
Expand Down
16 changes: 9 additions & 7 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@

.popup-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(10px);

display: flex;
justify-content: center;
align-items: center;
z-index: 1000; /* Ensure the overlay is on top */
}

.popup-content {
display: flex;
justify-content: center;
Expand All @@ -32,16 +33,13 @@
pointer-events: none
}

/* 浅色模式下的弹窗背景 */
.theme-light .popup-content {
background-color: rgb(255, 255, 255, 0.5) !important; /* 白色背景 */
color: #000000; /* 黑色文本 */
background-color: var(--background-primary) !important; /* 白色背景 */
}

/* 深色模式下的弹窗背景 */
.theme-dark .popup-content {
background-color: rgb(51, 51, 51, 0.5) !important; /* 深灰色背景 */
color: #ffffff; /* 白色文本 */
background-color: var(--background-primary) !important; /* 深灰色背景 */
}

/*开启弹窗按钮*/
Expand Down Expand Up @@ -107,6 +105,10 @@ div.mermaid-popup-button:hover, div.mermaid-popup-button-reading:hover {
color: var(--button-color-hover);
}

.setting-item-on-top-line{
border-top: 0px;
}

/*以下是配置中已存图表来源的表格样式*/

/* 定义 kv-row 的 flex 布局样式 */
Expand Down

0 comments on commit 0d68e5d

Please sign in to comment.