Files
compiler-explorer/static/widgets/fontscale.ts
Matt GodboltandClaude 637564f389 Migrate to Bootstrap 5 (#7582)
This PR completes the migration from Bootstrap 4 to Bootstrap 5.3.5
following the plan outlined in
[docs/Bootstrap5Migration.md](https://github.com/compiler-explorer/compiler-explorer/blob/mg/bootstrap5/docs/Bootstrap5Migration.md).

## Migration Process

We followed a phased approach as documented in the migration plan:

1. **Phase 1: Dependency Updates and Basic Setup**
   - Updated Bootstrap from 4.6.2 to 5.3.5
   - Added @popperjs/core dependency (replacing Popper.js)
   - Updated Tom Select theme from bootstrap4 to bootstrap5

2. **Phase 2: Global CSS Class Migration**
   - Updated directional utility classes (ml/mr → ms/me)
- Updated floating utility classes (float-left/right → float-start/end)
   - Updated text alignment classes (text-left/right → text-start/end)

3. **Phase 3: HTML Attribute Updates**
- Updated data attributes to use Bootstrap 5 prefixes (data-bs-toggle,
data-bs-target, etc.)
   - Fixed tab navigation issues

4. **Phase 4: JavaScript API Compatibility Layer**
   - Created bootstrap-utils.ts compatibility layer
- Updated component initialization for modals, dropdowns, popovers, etc.

5. **Phase 5: Component Migration**
- Updated and tested specific components (modals, dropdowns, toasts,
etc.)
   - Fixed styling issues in cards and button groups

6. **Phase 6: Form System Updates**
   - Updated form control classes to Bootstrap 5 standards
   - Updated checkbox/radio markup patterns
   - Simplified input groups

7. **Phase 7: Navbar Structure Updates**
   - Updated navbar structure with container-fluid
   - Fixed responsive behavior

8. **Phase 8: SCSS Variables and Theming**
   - Added custom CSS fixes for navbar alignment
   - Verified theme compatibility

9. **Phase 9: Accessibility Improvements**
   - Updated sr-only to visually-hidden
   - Added proper ARIA attributes
   - Enhanced screen reader support

## Key Changes

- No more jQuery dependency in Bootstrap 5
- New prefix for data attributes (data-bs-*)
- Improved accessibility with ARIA attributes
- Updated positioning classes (start/end instead of left/right)
- Simplified input group structure

## Test Plan

1. **Navigation Testing**
   - Verify all dropdown menus open and close properly
   - Test mobile menu responsiveness
   - Check tab navigation in settings dialog

2. **Component Testing**
- Verify all modals open and close correctly (settings, share,
load/save)
   - Test tooltips and popovers
   - Check form controls in different dialogs

3. **Layout Testing**
   - Test responsiveness on different screen sizes
   - Verify proper alignment of elements
   - Check dark mode compatibility

4. **Specific Features to Test**
   - Compiler selection and options
   - Share dialog functionality
   - Settings dialog
   - Tree view (IDE mode)
   - Font selection dropdown

5. **Browser Testing**
   - Test in Chrome, Firefox, Safari
   - Test in mobile browsers

## Note on Further Improvements

After this migration is stable, we could consider Phase 12: removing
jQuery dependency entirely, as Bootstrap 5 no longer requires it. This
would be a separate effort.

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-04-24 12:10:37 -05:00

148 lines
5.7 KiB
TypeScript

// Copyright (c) 2021, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import EventEmitter from 'events';
import $ from 'jquery';
import {editor} from 'monaco-editor';
import {options} from '../options.js';
import {Settings} from '../settings.js';
import IEditor = editor.IEditor;
import {FontScaleState} from './fontscale.interfaces.js';
function getDefaultFontScale() {
return Settings.getStoredSettings().defaultFontScale ?? options.defaultFontScale;
}
function makeFontSizeDropdown(elem: JQuery, obj: FontScale, buttonDropdown: JQuery) {
function onClickEvent(this: JQuery) {
// Toggle off the selection of the others
$(this).addClass('active').siblings().removeClass('active');
// Send the data
obj.applyScale($(this).data('value'));
obj.emit('change');
}
for (let i = 8; i <= 30; i++) {
const item = $('<button></button>');
item.attr('data-value', i).addClass('dropdown-item').text(i).appendTo(elem).on('click', onClickEvent);
if (obj.scale === i) {
item.addClass('active');
}
}
buttonDropdown.on('wheel', (e: any) => {
e.preventDefault();
let selectedId = elem.find('.active').index();
if (e.originalEvent.deltaY < 0 && selectedId < elem.children().length - 1) {
selectedId++;
} else if (e.originalEvent.deltaY >= 0 && selectedId > 0) {
selectedId--;
}
elem.children().eq(selectedId).trigger('click');
});
// ctrl+click handler
buttonDropdown.on('click', e => {
if (e.ctrlKey) {
// This is a hack. It prevents bootstrap's click listener from opening the dropdown
e.stopImmediatePropagation();
// Set the correct scale as active
elem.find('.active').removeClass('active');
elem.find(`[data-value=${getDefaultFontScale()}]`).addClass('active');
// Set the scale
obj.applyScale(getDefaultFontScale());
obj.emit('change');
}
});
}
function convertOldScale(oldScale: number): number {
// New low + ((new max - new low) * (oldScale - old low) / (old max - old low))
return Math.floor(8 + (22 * (oldScale - 0.3)) / 2.7);
}
export class FontScale extends EventEmitter.EventEmitter {
private domRoot: JQuery;
private fontSizeList: JQuery;
public scale: number;
private readonly usePxUnits: boolean;
private fontSelectorOrEditor: JQuery | string | IEditor;
private isFontOfStr: boolean;
constructor(domRoot: JQuery, state: FontScaleState & any, fontSelectorOrEditor: JQuery | string | IEditor) {
super();
this.domRoot = domRoot;
// Old scale went from 0.3 to 3. New one uses 8 up to 30, so we can convert the old ones to the new format
this.scale = state.fontScale || getDefaultFontScale();
// The check seems pointless, but it ensures a false in case it's undefined
// FontScale assumes it's an old state if it does not see a fontUsePx in the state, so at first it will use pt.
// So the second condition is there to make new objects actually use px
this.usePxUnits = state.fontUsePx === true || !state.fontScale;
if (this.scale < 8) {
this.scale = convertOldScale(this.scale);
}
this.setTarget(fontSelectorOrEditor);
this.apply();
this.fontSizeList = this.domRoot.find('.font-size-list');
makeFontSizeDropdown(this.fontSizeList, this, this.domRoot.find('.fs-button'));
}
apply() {
if (this.isFontOfStr) {
this.domRoot
.find(this.fontSelectorOrEditor as JQuery)
.css('font-size', this.scale + (this.usePxUnits ? 'px' : 'pt'));
} else {
(this.fontSelectorOrEditor as IEditor).updateOptions({
fontSize: this.scale,
});
}
}
addState(state: any) {
state.fontScale = this.scale;
state.fontUsePx = true;
}
applyScale(scale: number) {
this.scale = scale;
this.apply();
}
setScale(scale: number) {
this.fontSizeList.find('.active').removeClass('active');
this.fontSizeList.find(`[data-value=${scale}]`).addClass('active');
this.applyScale(scale);
}
setTarget(target: JQuery | string | IEditor) {
this.fontSelectorOrEditor = target;
this.isFontOfStr = typeof this.fontSelectorOrEditor === 'string';
this.apply();
}
}